Orange is the new Hash

Reblogged from Ruby 005 student Liz Baillie During my first two weeks at the Flatiron School, I was inundated with information. I spent so much time coding and learning about code that I actually started to dream about it. One night, I went to bed after watching one of the new episodes of Orange is […]

Reading Time 3 mins

Reblogged from Ruby 005 student Liz Baillie

During my first two weeks at the Flatiron School, I was inundated with information. I spent so much time coding and learning about code that I actually started to dream about it. One night, I went to bed after watching one of the new episodes of Orange is the New Black, and had an incredibly detailed dream about sorting the fictional Litchfield prison into a system of nested hashes.

I had been having a lot of trouble that week learning how to create and iterate over hashes, so I thought, “why not work on creating that Litchfield hash I dreamed about?” So, here we are.

Blog post image: tumblr_inline_n7bfpe1coF1rtan47.jpg

You can’t have a prison without COs, so let’s start with them. First, I created the Litchfield hash, and then added Mendez as a CO. However, I noticed that when I tried to add O’Neill as an additional CO, Mendez disappeared. What happened?

Blog post image: tumblr_inline_n7bfra407Y1rtan47.png

I realized that the COs key could only point to one thing, so I needed it to point to a single array in which I could store all the COs, otherwise every time I tried to an a new one he or she would override the previous one. We can’t have a prison with just one CO, can we?

Let’s start by assigning an empty array to the COs, so we can fill it up.

Blog post image: tumblr_inline_n7bfsiVpqS1rtan47.png

Then, we can hire some COs and push them into the array, like so:

Blog post image: tumblr_inline_n7bftcJmpx1rtan47.png

There must be a quicker way to do this. When we create the “prisoners” key, can we start right off by assigning it to an array of names, instead of creating an empty array and assigning them one by one? Yes, we can!

Blog post image: tumblr_inline_n7bfuxmbzl1rtan47.png

Awesome. Now we just need to add some administrative staff, which we can do like so:

Blog post image: tumblr_inline_n7bfvgubvk1rtan47.png

Great! Now we have ourselves a prison. But prison is not quite so simple. Unfortunately, sometimes prisoners get thrown in the SHU (solitary confinement). Chapman was just attacked by Pennsatucky with a shiv, and now Chapman’s in the SHU for fighting. Let’s have each of the prisoners’ names point to new hash where we can store their details, such as whether or not they are currently in the SHU.

I had a really hard time figuring out an easy way to turn each prisoner name string into a key pointing to a key/value pair of {:SHU => true} or {:SHU => false}. I spent way more time on trying to figure than out than I probably should have.

Blog post image: tumblr_inline_n7bfwxAYxI1rtan47.gif

Eventually I realized I could just create a separate prisoners hash with each name pointing to an empty hash, stick it inside an array, and use the replace method to replace the old array of strings with an array of hashes!

Blog post image: tumblr_inline_n7bfy3Rpuq1rtan47.jpg

First, the new hash created from the prisoners list:

Blog post image: tumblr_inline_n7bfz2Wj5f1rtan47.png

Then, stick that hash into a new array, and replace the old array with the new one:

Blog post image: tumblr_inline_n7bfziRX361rtan47.png

Next we get rid of that pesky array surrounding them so we can more easily manage them as a hash. We can do this by reassigning the first index of the prisoners array (which is the entire hash) to the Litchfield prisoners hash. This gets rid of those ugly brackets:

Blog post image: tumblr_inline_n7bg4bmXwb1rtan47.png

So when we call “Litchfield,” we now get this:

Blog post image: tumblr_inline_n7bg4vYjx21rtan47.png

Now we want to have Chapman’s hash to point to a new hash that describes her current SHU status, like so:

Blog post image: tumblr_inline_n7bg5uJI1d1rtan47.png

Finally, when we call Litchfield, we get this:

Blog post image: tumblr_inline_n7bg6awlOp1rtan47.png

Now, if we wanted to add any more details to anyone’s hash, we know how to do it!

Blog post image: tumblr_inline_n7bg74RCSC1rtan47.gif

Disclaimer: The information in this blog is current as of June 17, 2014. Current policies, offerings, procedures, and programs may differ.

About Flatiron School

More articles by Flatiron School