Orange is the new Hash

Flatiron School / 17 June 2014

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.

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?

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.

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

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!

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

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.

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!

First, the new hash created from the prisoners list:

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

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:

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

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

Finally, when we call Litchfield, we get this:

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

Project Recap: Heat Seek NYC Previous Post Alums Win LOLs at Comedy Hack Day Next Post