June 2017
Beginner
330 pages
7h 30m
English
You already learned how to iterate through a hash in one of our previous sections, however, it's a very critical task that you'll be using quite often. With that in mind, I'm going to discuss how it works in more detail. Also, I'm going to show you how you can iterate over only the keys or values.
I'm going to start with the hash we created in the previous section:
people = { jordan: 32, tiffany: 27, kristine: 10, heather: 29 }
When you want to only grab the keys, your code can be like this:
people.each_key do |key| puts key end
If I run this code, it prints out each one of the keys like this:

Read now
Unlock full access