DEV Community

Cover image for Hash-tastic! part one

Posted on

Hash-tastic! part one

Photo by Márcia Rodrigues from FreeImages

For many of us, hashes can be intimidating. Just when you thought you had mastered arrays, here come hashes with their snazzy curly brackets and all that extra syntax. Such show-offs!

Really though, as the information you are working with grows larger, it is important to be comfortable with accessing and manipulating data stored in collections. It will be common for you to see nested data structures of many types used: arrays of hashes, hashes of arrays, hashes of hashes, just to name a few. Looking at all that data packed away inside those nested brackets, curly and otherwise, can seem overwhelming and as a newbie you may be left feeling confused. Fear not! Hashes are your friends.

Let's talk about hashes and how to break them down into understandable pieces. I like to think of hashes as well-arranged book shelves where the contents of each shelf can be organized into key/value pairs.

Take a few moments to really understand the following hash:

Alt Text

It looks pretty complicated, but if you break it down piece by piece, you will see that it isn't as scary as you thought!

Run the below to see how to work with the first level of this hash.

To reach into the first level of this hash so that you can view the contents of each shelf is easy! Just type the name of the hash with square brackets around the 'shelf', or key name. Like this: bookshelf[:shelf_name]

Take a few minutes to check out the again and consider what type of data each shelf returns. Feel free to explore the code to gain a better understanding of this hash as we will be discussing it in more detail over the next two parts of this series.

By the way-
That last bit about the Harry Potter shelf is quite messy! No worries! We will take a look at how to better display that data in part three of this series.

I did want to discuss the method on line 44 a little bit...
As the hash is written, each first-level key is a symbol that includes an underscore which is not very pretty when put out to the user. To be more user-friendly, I needed to change ':shelf_one' to 'Shelf one' so that the sentence to be output to the user would make more sense.

To do this, I first had to convert the symbol to a string with the .to_s method. Then I used .capitalize to capitalize the string, and then to take away that pesky underscore, I used .split('_') and finally .join(' ') to get the separate strings to behave as one.

See you next time for part two in this series!

Top comments (2)

burdettelamar profile image
Burdette Lamar

You could use symbol :"Shelf one".

leighad profile image

Yes! Thanks Burdette- I could have. I wanted to work with the original data set, similar to what one may encounter when working with someone else's code. I did think about going back and changing the keys in my hash, but then I thought that the example may be fun for others to see. Thanks for the great comment and have a wonderful day!