From 7c34268f848a359132874a285acc54cb704216c9 Mon Sep 17 00:00:00 2001 From: Dan Dorman Date: Thu, 8 Sep 2011 09:46:02 -0600 Subject: [PATCH] Add koan addressing Hash#fetch. --- koans/about_hashes.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/koans/about_hashes.rb b/koans/about_hashes.rb index 2324b04..7662488 100644 --- a/koans/about_hashes.rb +++ b/koans/about_hashes.rb @@ -20,6 +20,18 @@ class AboutHashes < EdgeCase::Koan assert_equal __, hash[:doesnt_exist] end + def test_accessing_hashes_with_fetch + hash = { :one => "uno" } + assert_equal "uno", hash.fetch(:one) + assert_raise(___) do + hash.fetch(:doesnt_exist) + end + + # THINK ABOUT IT: + # + # Why might you want to use #fetch instead of #[] when accessing hash keys? + end + def test_changing_hashes hash = { :one => "uno", :two => "dos" } hash[:one] = "eins" -- 1.8.0.2