
Since I have been writing a lot about hashes I thought why not go into a little bit more detail about hashes. What if a hash contains an array? What if that array has more hashes inside of it? Let’s see how this will look and how to access an element inside of it…

There is a lot going on inside of this hash! Step by step of what this is:
- The name of the hash is “favorite_foods”
- The first key is “fruits” and the value is an array. Inside the array there are two more hashes.
- The next key we see is “candy” and it also has an array as the value. Inside the array are two more hashes.
How would we print out all the different fruits and what colors they are?

This is going to print out your “fruits” which are: [{“apple” => “red”}, {“grapes” => “purple”}]
You can tell when it is all by itself that it is an array with two hashes inside it.
What if we only wanted to print out “red” for “apple”?

- First we have written on the top line what we just saw only now we are having that value set to a variable called “fruits_array”. (This means that since favorite_foods[“fruits”] will return the array of fruits we are now saying put that array and store it into “fruits_array”).
- Now that we have the array stored in a variable we can apply the EACH method to it. The each method can take a block which is passed through the pipes (these guys ||).
- Inside the pipes (||) I wrote “fruit_hash”, because we are going to be working with the hashes now for fruits. It is really the same thing that we saw before when we wrote: favorite_foods[“fruits”].
- Then you will see printed to the screen….red!!






