Top Hash Methods in Ruby
Since hashes are an integral part of Ruby, I want to go through some helpful methods that can be quite handy in development.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Since hashes are an integral part of Ruby, I want to go through some helpful methods that can be quite handy for development projects.

We are going to use the same people hash that we have been working over the last few lessons:

people = {jordan: 32, tiffany: 27, kristine: 10, heather: 29}

Adding Element to a Hash

First, we are going to see how to add items to a hash. To do that, add in the following code:

people[:leann] = 42

Now, if you type people, you can see this new element has been successfully added.

large

So adding to a hash is similar to adding to arrays. But instead of passing in an index and value you supply a key and then a value.

Swap Keys and Values

Next, we are going to see how to reverse the key-value pair values. For our example let's imagine that we want to swap out the hash so that the age is the key and the name is the value.

One way to do is to iterate through each pair, store it in a variable, swap them and update. But, that's slow and more tedious. A better and easier way would be to leverage Ruby's invert method:

people.invert

This does the swapping work for us.

{32=>:jordan, 27=>:tiffany, 10=>:kristine, 29=>:heather, 42=>:leann}

This method can be particularly useful if you want to store the inverted values into a new hash, which we can do with code like this:

people_2 = people.invert

Merging Hashes

Next, we are going to see how to merge the two hashes, people and people_2 together:

people.merge(people_2)

The output is:

large

Convert a Hash into an Array

Next, we are going to see how to convert a hash into an array. We can leverage the built in Ruby conversion array conversion method to accomplish this for us, as you can see below:

Array(people)

devCamp Note: Ruby has a number of data type conversion methods. For example to convert a hash to an array you could use either Array(people) or people.to_a. The differences are subtle and for the majority of cases either option will work for your needs.

The output of this code is:

large

In taking a look at our newly rendered array, each key-value pair has been converted to an array nested inside one large array that contains all of the pairs. This method gives you the flexibility to swap between an array and a hash based on your program needs.

Listing all Keys

So what happens if you want to see all of the keys in a hash? You could iterate through the hash with the each_key method that we've used before. However you can also get the list of keys by simply calling this method:

people.keys

This method will list out all of the keys for you and place them into an array.

[:jordan, :tiffany, :kristine, :heather, :leann]

This is great for debugging, however, if you want to do something with each key, then iterating through the hash with the each_key method most likely would be a smarter choice.

Listing All Values

In like manner you can see all of the values with the values method:

people.values

Running this code will output:

[32, 27, 10, 29, 42]

Note that both the keys and values hash methods return the elements as an array. So if you want to store all the keys or all the values in an array, this is a great way of doing it.