From 12542bed6409e509a0ab7b451c9711f16bb6eea3 Mon Sep 17 00:00:00 2001 From: Asif S Kalam Date: Thu, 5 Jan 2012 17:22:46 +0530 Subject: [PATCH 1/2] Added new section on Hash#select. Bettered the text in the introduction to hashes example. --- .../01 - Introduction to Ruby Hashes.haml | 8 ++--- .../02 - Iterating over Hashes.haml | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) 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..c54ff54 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", 170 + %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 From 5a515fc259c9661eb8d42c0f16f3ba8a66d211f3 Mon Sep 17 00:00:00 2001 From: Asif S Kalam Date: Wed, 1 Feb 2012 11:27:27 +0530 Subject: [PATCH 2/2] Leave out section ID. --- docs/Hashes in Ruby /02 - Iterating over Hashes.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Hashes in Ruby /02 - Iterating over Hashes.haml b/docs/Hashes in Ruby /02 - Iterating over Hashes.haml index c54ff54..b61a389 100644 --- a/docs/Hashes in Ruby /02 - Iterating over Hashes.haml +++ b/docs/Hashes in Ruby /02 - Iterating over Hashes.haml @@ -103,7 +103,7 @@ restaurant_menu.keys DATA !release -.section :selecting_values_from_a_hash, "Selecting values from a Hash", 170 +.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.