Traversing the Internets

Web Development in NYC

Flatiron School Day Eight

I came out of the weekend feeling like I really knew what I was doing, then Monday happened. At the beginning of the day I was feeling good about my competence level but once we really dug into nested hashes and how to access data inside of them the wheels really came off the rails for a while.

The first few hash projects I was able to get through cobbling together nested do blocks based on .each and the key varient but it was really messy. I felt like I was trying to break a problem down into simpler ones like Avi told us, but coming up with even more complicated answers that usually didn’t work.

Then came Hashketball. A giant monstrosity of nested hashes that we had to find a bunch of ways of iterating through to get to the particular piece of data we wanted. At times we also had to compare various types of data that could be found throughout the nested hash jungle, all while trying to avoid the local predators (read: errors, fatigue, frustration). After what seemed like a day and a half but was really probably only a couple hours of fruitlessly banging on my keyboard trying to break the problem down into smaller problems, or try and get elaborate nested blocks to do what I wanted I finally found the heartbreakingly simple answer of how to access the hash how I wanted.

It was so simple I couldn’t believe it. Or at least I couldn’t believe it until the thought occured to me that rubyists likely have to do this sort of thing and since its ruby there has to be an easy way to do it, that’s just the way ruby works.

It looks so simple when you see it…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
hash =
{
  :hash1 =>
  {
    :hash2 => 1,
    :hash3 =>
    {
      :hash4 => 2,
      :hash5 => 3
    },

  },
  :hash6 => 4
}

#so to access the number 3.

hash[:hash1][:hash3][:hash5]

#this looks at the 'hash' created, then accesses deeper levels of the hash by 
#calling on keys in [].


#you can also iterate through pieces of a hash.

hash[:hash1][:hash3].each_key do |key|
  hash[:hash1][:hash3][key]
end

#This looks through the has to the key :hash3 then looks at each key within and 
#returns the value it's associated with.

Comments