Hi,
Here are a couple of hints for this week's assignment.
A hash is a good way of storing pairs of objects that are related to each other in some way. The book gives states and their abbreviations as good examples of this:
h = {'Massachusetts' => 'MA', 'New York' => 'NY'}
To get the abbreviation of Massacusetts, you'd use the following:
puts h['Massachusetts'] # MA
puts h['New York'] # NY
To add another value to the has after it's been create, do this:
h['New Jersey'] = 'NJ'
puts h['New Jersey'] # NJ
But referencing a key that isn't already in the has returns a nil object.
puts h['Utah'] # nil
You can use a hash to count the number of times each element appears in a given array. Can you think of how to do this?
Look for Hint #2 later in the day...
Glenn
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment