Iterating over Nested Hashes Codealong
Objectives
Iterate through a nested hash
Modify the correct element in a nested hash
Why Nested Hashes Matter
So much of what we do in programming involves storing data in hashes. Often the hashes that we will encounter will have more than one level. As we get into the web, this will become abundantly clear. To build programs in the future, we'll absolutely need to get comfortable working with hashes. Let's get started!
Code Along Exercise
Fork and clone this lab. You'll be coding your solution in lib/contacts.rb
. You'll be manipulating the following Hash
:
Your good buddy Freddy Mercury has recently developed a strawberry allergy! You need to delete "strawberry"
from his list of favorite ice cream flavors in the remove_strawberry
method.
Iterate over the contacts
hash and when you reach the key :favorite_ice_cream_flavors
, remove "strawberry"
from the Array of Freddy's favorite ice cream flavors.
There are at least two ways you can accomplish this, and for this codealong, we'll work with the second way.
You can directly iterate over the hash that is the value of the
"Freddy Mercury"
key by calling an enumerator method incontacts["Freddy Mercury"]
.You can set a conditional to iterate through the hash for
Freddy Mercury
only and when you reach the appropriate level, check to see if the key==
("is equal to"):favorite_ice_cream_flavors
. If it does, check to see if that array contains"strawberry"
. If it does, then delete it from theArray
.
Step 1: Iterate over the first level
Inside the remove_strawberry
method, let's take our first dive into the contacts Hash
. Then we'll use binding.pry
to see where we are.
We are going to first iterate over the top level of the Hash
where the keys should be the person and the values should be a Hash
of details about the person.
Note on variable naming: This process will be remarkably easier if you name your variables to accurately reflect the data they represent. For now, when the value we're iterating over is another hash, we will explicitly add a _hash
to the end of the variable name (E.G. contact_details_hash
below).
In the terminal, let's hit the pry
by running learn
, and check that our defined variables (person
and contact_details_hash
) match our expectations.
Excellent! They do!
Type exit
while in pry to continue (a second pry
will trigger since we have two contacts). Running learn
will also display a test, which we haven't passed just yet.
You can also run ruby lib/contacts.rb
in the terminal - instead of displaying the the test results, this will reach the binding.pry
.
Step 2. Iterate over the second level
Again, let's jump into our binding.pry
using learn
. You should see:
Step 3. Locate the element we're looking for
What is data
when we hit the binding? If it's unclear, let's go into our binding.
Step 4. Update the hash
Congrats! You made it. Test that your method works by running ruby bin/contacts
in the terminal. It should output the hash without strawberry ice cream. Also, be sure to run the specs to make sure they pass.
Last updated