diff --git a/docs/Hashes in Ruby /01 - Introduction to Ruby Hashes.haml b/docs/Hashes in Ruby /01 - Introduction to Ruby Hashes.haml index a5fdf5f..9e9e00a 100644 --- a/docs/Hashes in Ruby /01 - Introduction to Ruby Hashes.haml +++ b/docs/Hashes in Ruby /01 - Introduction to Ruby Hashes.haml @@ -7,7 +7,7 @@ title - Introduction to Ruby Hashes 'associative arrays', 'dictionary', 'HashMap' etc. in other languages %p A blank hash can be declared using two curly braces {}. Here is - an example of a Hash in Ruby: + an example of a Hash, with some items in it: %p %pre @@ -65,13 +65,13 @@ restaurant_menu = { .section :fetch_from_a_hash, "Fetch values from a Hash", 167 %p - You can retrieve values from a Hash object using [] operator. + You can retrieve values from a Hash object using the [] operator. The key of the required value should be enclosed within these square brackets. %p Now try finding the price of a Burger from the restaurant_menu hash: - write the name of the object, follow it with a square bracket, and place the - key inside the brackets. In this case the key is a string so enclose the key + write the name of the hash, and place the key inside the []. + In this case the key is a string so enclose the key in quotes. !enchant 3339 diff --git a/docs/Hashes in Ruby /02 - Iterating over Hashes.haml b/docs/Hashes in Ruby /02 - Iterating over Hashes.haml index 1b3791a..b61a389 100644 --- a/docs/Hashes in Ruby /02 - Iterating over Hashes.haml +++ b/docs/Hashes in Ruby /02 - Iterating over Hashes.haml @@ -102,3 +102,38 @@ restaurant_menu.keys pp user_code DATA !release + +.section :selecting_values_from_a_hash, "Selecting values from a Hash" + %p + You can select key-value pairs from the hash, which satisfy a condition - using the select method. + It will return a new hash with just the items which satisfied the condition. + + %p + Try getting items which cost less than 10 from the restaurant_menu hash: + + !enchant + exercise! + short_name :selecting_values_from_a_hash_examples + starting_code <<-DATA +restaurant_menu = { "Burger" => 5, "Pizza" => 12, "Coffee" => 1 } +# your code here + DATA + + specs <<-DATA + it "picks items which cost less than 10" do + user_code.should == {"Burger" => 5, "Coffee" => 1} + end + DATA + + solution <<-DATA +restaurant_menu = { "Burger" => 5, "Pizza" => 12, "Coffee" => 1 } +restaurant_menu.select { |item, price| price < 10 } + DATA + + code_wrapper <<-DATA + def user_code + <%= user_code %> + end + pp user_code + DATA + !release