Wednesday, 1 October 2014

Ruby Basics » Part 15 | Hashes - A Quick Intro

Welcome to the first post of the second part, in our series on Ruby Basics. To see what's coming up, check out the Trello board:


When we looked at Arrays, we saw that collections of data were stored under a given Array name. These were accessed by knowing the integer value of the data item’s location in the array. If you recall in the Basics 1 Wrap Up, we had the following Array:

        rolesArray = ["Developer", "Tester"]

To access these we need to use [0] and [1], as in

        print rolesArray[0]
Later on we assigned David or Alan as one of these roles and this worked fine. But what if we now wanted to assign them individual salaries, periods of employment, holidays allocated or other relevant data. We could create Arrays and put the data in the same sequence as the employee array. For example we might set-up:

rolesArray = ["Developer", "Tester"]
rolesHolder = ["Dave", "Alan"]
rolesSalary = [40000, 35000]
rolesHoliday = [25, 25]
I’m guessing you can see that’s all well and good if everything stays in order. For those with a little Database knowledge this problems with the above approach scream even louder. What we need is a way to explicitly pair the data above with a key bit of data that won’t change. In this case that key bit of data is the employee name.  How can we label the various bits of data with the employee name?

What we need is a key --> value pairing of data, so no matter what order they are stored, we can find, edit, update, and delete the correct one. As luck (Ruby) would have it, what we need is a Hash.


A Hash is a collection of Key-Value pairs. Hashes are also referred to as associative arrays, maps and dictionaries. They’re like Arrays in that they’re still variables storing variables, however unlike Arrays the data isn’t stored in a particular order. Also unlike Arrays, we don’t push/pop data into and out of the Hash, we simply insert and delete Hash values. Let’s look at making a Hash for some of the above data.

We can make a new empty Hash in a similar way to a new empty Array;

        rolesHash =

If we print the above, of course nothing will be returned. As we then acquire data to add to it, we can insert the data by giving the key-value pairs:

        rolesHash["David"] = "Developer"

Try running the entire snippet below:

rolesHash =
puts rolesHash

rolesHash["David"] = "Developer"
rolesHash["Alan"] = "Tester"

puts rolesHash

Here we add two key-value pairs to our newly created Hash and print the entire Hash out, which looks something like this:

        {"David"=>"Developer", "Alan"=>"Tester"}

If we wanted to find out what role David was currently in we could look it up using the key:

                    puts rolesHash["David"]

If you try this with a name that is not in the Hash, then the result will be nil which isn’t very informative. A better way is to define a default value, for example:

                    rolesHash ="No Role Assigned")

Try it again and watch the default message get printed.

Adding to the Hash is good, but we also need to delete items too. To do this we simply call the delete method on the hash and specify which key we want deleting.


If you’d prefer to just build out your Hash from the start, you can do that too.

salaryHash = Hash["David" => 30000, "Alan" => 35000]
puts salaryHash

We’ll leave the basics of Hashes there, as always have a look at the Ruby docs to see more of the methods available. Later on, we’ll look at some of the more complex aspects of Hashes, but for now we have what we need!