Ignition: Week 4

Sorry I am late in posting this week’s review. It has been a hectic 2 weeks for sure.

Week 4 was all about compound data structures, and while now it seems easy now, at the time it was super difficult and made me question whether or not I could even pull off this whole Programmer gig. Surprise, I can.


It’s funny, I just reviewed my post for Week 3 and it says we learned Hash’s and .each iteration loops among other things. But, that can’t be right! It feels like I’ve been doing .each loops for YEARS! Honestly, I am dreaming about each loops almost every night. If you’re a psychologist and looking for pro-bono work (student here) please contact ASAP.

On to the more advanced stuff.


So, the bulk of this week was comprised of going over Compound Data Structures. How they are built, how to structure data depending on how you want to access it to make it easiest to access later on, and how to work your way through these data structures.

Last week we had Hash’s. This week we have An Array of Hashes & Hash of Hashes. Not as complicated as you may think. Remember how an array is constructed? new_array = [1,2,3,4,5]What if we want to access the first element of this array? puts new_array[0].Remember this is a 0-based index, so element 1 is 0, element 2 is 1 and so on.

What if we have an array of hashes, what would that look like?

golf_score screenshot.png

In this instance, we have an Array named golf_game which contains 18 hashes. If you’re getting tripped up by all the nonsense going on inside of a Hash try to think of them as more detailed Array’s (which they kind of are). In an Array if we want to assign a value, we just call on the index (eg new_array[1]) but in a hash, we call on the key to return the associated value. In the example above, in hash 1 the keys would be hole, par & score  and the values would be 1, 5 & 7.

If these hashes were not in an array they would be named,

hash_one = {hole: 1, par: 5, score: 7}

and to call on our values we would do the following.

puts hash_one[:hole]  – It’s worth noting that you use [] instead of () or {}when calling on the key of the hash.

You see, the key, is what we use to access the value, which is the most valuable part


What about a Hash of Hashes? …


You see, there are instances where a Hash of Hashes makes good sense. We have a Hash for People, but we don’t want to stop short at just listing these people, we want to list specific attributes about them and do it for every person, enter the Hash of Hashes.

In our first example, we access a value in the nested Hash directly. we do this by first declaring the first hash (in this case people). Then we append to that the name of the next hash ['mike'] then after that the key  [:age] . In this case we are making :age SYMBOL but it could just as easily be a string ‘age’. 

In our next example, we are only printing out the names of the nested hashes inside our people hash. 

In the last example, we are printing out name of our hashes followed by the key and value per each line in our command line. It is important to remember that each hash has a key/value pair associated with it, ALWAYS.

So in our first instance, the key/value pair is as follows: 

key is mike and tonyrobbins & value is the entire hash!

e.g. {:age => 29, :good_looking => 'Incredibly', :job => 'Web Developer'}

In our second run of the each loop we are now at the level of mike / tonyrobbins. And in this instance, the keys for each person are the attributes of  age, good-looking & joband the values are the values associated with those keys!


A lot to process right now, I know. But this is the backbone as I see it of Compound Data Structures. If you can understand how to work your way through these structures thing will open up to you immensely later on in your learning!

It is possible to leverage these simple tactics to store and arrange data for different projects. At first it will be overwhelming but later it will come easy. Trust, just Trust! And to think, we haven’t even hit the array of hashes or hash of hash of hash of hash of hashes. Just joking about that one, I think. I’ll let you know when I graduate in 3 months.

And remember, as always…


Check out these resources:

One comment

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s