Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new section on Hash#select. Bettered the text in the introduction to hashes example. #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/Hashes in Ruby /01 - Introduction to Ruby Hashes.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>{}</code>. Here is
an example of a Hash in Ruby:
an example of a Hash, with some items in it:

%p
%pre
Expand Down Expand Up @@ -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 <code>[]</code> operator.
You can retrieve values from a Hash object using the <code>[]</code> 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 <code>restaurant_menu</code> 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 <code>[]</code>.
In this case the key is a string so enclose the key
in quotes.

!enchant 3339
Expand Down
35 changes: 35 additions & 0 deletions docs/Hashes in Ruby /02 - Iterating over Hashes.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>select</code> 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 <code>restaurant_menu</code> 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