June 6, 2017
Ruby on Rails Tutorial No Comments

Hash and Symbols – Ruby on Rails Tutorial

A hash is an unordered, object-indexed collection of objects( or key value pairs ). The only difference between hash and array is that array is an ordered collection whereas hash is an unordered collection. We cannot count an order for the things that are going to be stored in a hash. Instead of keeping track of elements in hashes by integer index by position, we keep track with object. Hashes are like dictionary in other programming languages. We have a key value pair. That means we have a label and a value. A label or key may not always be a number, it can be string or anything. We don’t have any order for those labels, that’s why we said a hash is unordered collection.

Let’s take an example of a book. A book has page numbers and each page is indexed by page number. You can think this as an array (ruby array tutorial can be checked here). But hash is little different. Say we don’t have page numbers in a book, then we need to find contents in the book by using the chapters. That is exactly what hash is. We also call it dictionary because in dictionary we have word and meaning, similarly in hash we have key and value.

Array is still useful if we need to preserve an order of collection. We use hash when label really matters.Another example, say we have a student attendance book. Then the roll number is a index and names are value. We need to preserve the order here. So we use an array. But take another example of student result and we want to know if a student  pass or not. For this we use hash because order is not important here. The key is name of student and value is either pass or fail.

Let’s create one now and see why hash is important. Let’s create an array for the same purpose and see what additional advantage hash provides to us. Open up your irb console and create a new array called person

$ irb

>> person = [‘Rabin’,’Poudyal’,’male’,22,’blue’]


So here, it created a little confusion we have item blue. But we don’t know if that defines the color of my eyes or color of shirt I am I wearing on. So we need to define hashes in these scenarios.

So to remove the confusion we use hash. Let’s define a hash.

person = { }

A hash is defined by curly braces. If you see curly braces as the data structure in ruby then it is a hash.Now let’s add key value pairs in this hash. We are going to structure our key value pair in this hash like following

person = {‘first_name’ => “Rabin”,’last_name’ => “Poudyal”,’age’ => 23, ‘eyes_color’ => ‘blue’ }

Now to access the element in the hash


=> “Rabin”


We can also find a key using value. Most beginner programmers get stuck when they are asked to do this.This is not very often, but let’s take a look



We can also have a hash that is of mixed type. Let’s take an example

mixed_type = {1 => [‘d’,’e’,’f’], ‘hello’ => ‘USA’, [2,4] => ‘string’}


Now find things by label in this mixed hash

>> mixed_type[[2,4]]

>> ‘string’

>> mixed_type[1]

>> [‘d’,’e’,’f’]


Some methods you can use in hash.

To get all of the keys in hash


>> mixed_type.keys

>> [1,’hello’,[2,4]


Similarly you can also find all the values

>> mixed_type.values

>> [[‘d’,’e’,’f’],’USA’,[2,4]]


To find the length of hash

>> mixed_type.length

>> 3

>> mixed_type.size

>> 3


To convert hash to array you can use the following method

>> mixed_type.to_a

>> [ [1,[‘d’,’e’,’f’]], [‘hello’, ‘USA’], [[2,4], ‘string’] ]


To clear the hash or empty the hash

>> mixed_type.clear

>> { }


Or you can also initialize it to empty hash again

>>mixed_type = {}

You can also add a new key value pair or update it by using following

>> mixed_type[‘mylabel’] = ‘value’

Now if you query this, you will see the new pair added to hash.

Now let’s see what symbols are. It is one of the most misunderstood data types by beginning Ruby Programmers. So I want to discuss this with hash. The symbols looks like string but they are not, they look like variables but they are not.The symbol is

  • A label that is going to identify the piece of data
  • It is going to be stored in memory one time whereas if we use string as our label it is going to store each time we initialize the label.

The way we define a symbol is by putting : (colon) just before the name. And name works the same way the variables would. Let’s create a symbol called my_test

>> :my_test

Let’s check what this object is of class of? To do this enter the command

>> :my_test.class

And it returns Symbol class. The string and symbol are not same, we can verify this statement using the following methods that Ruby provides us.

>> “my_string”.object_id

It returns some object id. Now let’s try to create a symbol :my_string and see its object id

>> :my_string.object_id

This must give us a different result right? If you again run the previous statement of string to get the object id then it would again print unique id but if you run the object id method with symbol again then it would print the same object id as before. So we can verify that symbols are very important when we want to write programs that are memory efficient. We can use symbols with hashes. See the example below

>> my_hash = { :first_name => “Rabin”, :last_name => “Poudyal” }

You must have seen this structure a lot while doing Ruby On Rails.



Hashes are very much used in Ruby and Ruby On Rails. Without having good understanding of how hashes work, one cannot be a good Ruby or Ruby On Rails programmer. There are a lot of things you can do with hashes. Symbols are like additional feature to hashes that you can integrate into your code to make memory efficient programs. You can check the ruby documentation to learn more about them.