Ruby Hash Tutorial
For complex collections, the **Hash data structure** is a powerful tool in Ruby programs. Hashes are **key/value** based and let you access data elements with more than a pure index, such as with arrays.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

For complex collections, the Hash data structure is a powerful tool in Ruby programs. Hashes are key/value based and let you access data elements with more than a pure index, such as with arrays. Though we've briefly touched on hashes in the previous lessons, I want to give you a firm understanding of what hashes are and how they can be used.

Essentially, a hash is a key-value pair collection. In an array, each element is a single item like a number or word whereas in a hash, each element has two items, and includes a value and a key associated with it. Sometimes, the value itself can be another collection like an array or a hash.

Ruby Hash Code Example

Let's start by creating a hash:

positions = {
                first_base: "Chris Carter",
                second_base: "Jose Altuve",
                short_stop: "Carlos Correa"
            }

When you run this code you can see the hash, where the keys are mapped to their respective values.

large

In the written code I placed each key/value pair on their own line for readability purposes, however if you're following along with the video guides you'll see that I used the irb console and built the hash on a single line. This is an important item to know because the Ruby parser doesn't care about white space, so both options will work fine. However when you're building code based programs it's considered a best practice to place each element on its own line.

If you see the output:

{:first_base=>"Chris Carter", :second_base=>"Jose Altuve", :short_stop=>"Carlos Correa"}

Ruby has automatically inserted the symbol : in front of every position to denote that it's the key and it is mapped to a value with the symbol =>, which is called a hash-rocket.

Though the above code is the most modern syntax to create a hash, there are other ways to create one too. It's important to understand the different ways to create a hash, especially if you're going to be working with existing Ruby applications since you will most likely see both options in the wild.

positions = { "first_base" => "Chris Carter", " second_base" => "Jose Altuve",  "short_stop" => "Carlos Correa" }

If you run it, the output will be the same as when we used the first syntax. Another way to create a hash would be:

positions = { :first_base => "Chris Carter", :second_base => "Jose Altuve", :short_stop => "Carlos Correa" }

So, all these three syntaxes will generate the same behavior, which syntax you go with is up to your own preference. Typically I utilize the first colon based version.

Next, let's see how to select an item from a hash that you've created.

positions = {
                first_base: "Chris Carter",
                second_base: "Jose Altuve",
                short_stop: "Carlos Correa"
            }

positions[:second_base]

If you run this code you can see that it will output player name Jose Altuve.

large

The easy way to remember how to grab items from a hash is that you use the key to lookup the corresponding value, and you pass it in as a symbol. This feature is one of the reasons why hashes are so popular for developers. Notice how you don't have the same flexibility with arrays. With arrays, if you want to grab an element from the collection, you either have to know its position in the array or you have to iterate through it until you find the item you want to select.

However with hashes we can lookup items based on its key, which is more practical in many different cases. Having a clear understanding of the differences between hashes and arrays is important so you can decide when to use each type of data structure.

Now, I'm going to show you what this looks like in a real life application.

large

If you look at the index method, you can see that it calls a method called paginate. This method allows our application to have pagination links. Looking inside the list of arguments passed to the method you'll see that we're passing a value called params[:page]. Does this syntax look familiar? The Ruby on Rails framework stores all of the parameters for a page in a hash structure. And by calling params[:page] we can tell our pagination process what page to look for.

I hope this gives you an idea of how to get started with hashes, in the next few guides we'll explore various ways we can work with this data structure.