4.1 Hashes, in and out.

Iterating over a Hash

You can use the each method to iterate over all the elements in a Hash. However unlike Array#each, when you iterate over a Hash using each, it passes two values to the block: the key and the value of each element.

Let us see how we can use the each method to display the restaurant menu.

Example Code:

Output Window

The restaurant is doing well, but it is forced to raise prices due to increasing costs. Use the each method to increase the price of all the items in the restaurant_menu by 10%.

Remember: in the previous example we only displayed the keys and values of each item in the hash. But in this exercise, you have to modify the hash and increase the value of each item.

Hint

This is how you find the increased (10%) price: price + (price * 0.1) - just keep in mind that whatever approach you use, make sure that the numbers are Floats and not Integers or the tests will fail. You will need to perform this calculation within the each block for the price of each item. Remember to store the changed value back into the Hash!

Output Window

Ideally, any transformation of a collection (like in the example above) should produce a new collection with the original unchanged making the code easier to understand and manage.

However, speed and memory considerations often (and usually wrongly) trump maintainability and so the approach above is used quite frequently.

Extracting the keys and values from a Hash

Every Hash object has two methods: keys and values. The keys method returns an array of all the keys in the Hash. Similarly values returns an array of just the values.

Try getting an array of all the keys in the restaurant_menu hash:

Output Window

Newer, faster.

There are some little-known shortcuts for creating new hashes. They all provide a slightly different convenience. The latter two generate a hash directly from pre-existing key-value pairs. The first simply sets a default value for all elements in the hash. Let's take a look at that first.

Example Code:

Output Window

As you can see, where a "normal" hash always returns nil by default, specifying a default in the Hash constructor will always return your custom default for any failed lookups on that hash instance.

The other two shortcuts actually use the Hash class's convenience method: Hash::[]. They're fairly straight-forward. The first takes a flat list of parameters, arranged in pairs. The second takes just one parameter: an array containing arrays which are themselves key-value pairs. Whew! That's a mouthful. Let's try the first form:

Example Code:

Output Window

Cool. And easy! You have to now use the second form the "new hash" shortcut in this exercise. Again, it takes an array of key-value pairs. These key-value pairs will just be 2-element arrays. I'll give you the key-value pairs to start with. Your objective is to build a Hash out of this array.

Hint

Try putting `a`, `b`, and `c` into the `key_value_pairs` variable as an array.

Output Window

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2024 by Sidu Ponnappa and Jasim A Basheer