Close
Close

Hashes in Ruby


Hash is the most important data structure of Ruby. In Python, it is known as dictionary. Like an array, it is also used to store data but in a bit different and quite useful way. So, let's discuss hashes.

Suppose, the cost of a mango is 40
the cost of a banana is 10
the cost of a cherry is 20
To store them, we can use an array. But there is an another good way to store all these.
Like, an array is []
a hash is {}
Let's code using hash.

fruit = {'mango'=>40,'banana'=>10,'cherry'=>20}
Output

The above code means that 'mango' is related to 40, 'banana' to 10 and 'cherry' to 20.

We access the elements of an array 'a' with a[0], a[1] and so on. For a hash, we do this:

fruit = {'mango'=>40,'banana'=>10,'cherry'=>20}
puts fruit['mango']
puts fruit['banana']
Output
40
10

Here, 'mango', 'banana', etc. are called keys.
40, 10 and 20 are values.
Let's see one more example of hash containing elements of different types.

a = {'x'=>[1,2,3,4],'y'=>"Hello World",'z'=>4.0}
Output

Here, 'x', 'y' and 'z' are the keys and their values are [1,2,3,4], "Hello World" and 4.0 respectively.

We can also change a value corresponding to a key. An example on this is given below.

a = {'x'=>[1,2,3,4],'y'=>"Hello World",'z'=>4.0}
a['x'] = "Hello"
puts "#{a}"
Output
{"x"=>"Hello", "y"=>"Hello World", "z"=>4.0}

In the above example, a['x']="Hello" changed the value of the key 'x' in the hash 'a' to "Hello".

Using loop on hash


Yes, we can also iterate over the whole hash using 'for' or 'each' loop. Look at the following example to see how we can do this.

a = {'x'=>[1,2,3,4],'y'=>"Hello World",'z'=>4.0}
# using for
for i in a
  puts "#{i}"
end

#using each
a.each do |i|
  puts "#{i}"
end
Output
["x", [1, 2, 3, 4]]
["y", "Hello World"]
["z", 4.0]
["x", [1, 2, 3, 4]]
["y", "Hello World"]
["z", 4.0]

Useful methods for hash


These methods are very useful when you are dealing with a hash. So, read and understand every example.

keys and values


'keys' and 'values' methods return all the keys and the values respectively of a hash in an array.

a = {'x'=>1,'y'=>2,'z'=>3}
puts "#{a.keys}"
puts "#{a.values}"
Output
["x", "y", "z"]
[1, 2, 3]

has_key?(key)


Checks if a key is present in a hash or not.

a = {'x'=>1,'y'=>2,'z'=>3}
puts "#{a.has_key?('x')}"
puts "#{a.has_key?('d')}"
Output
true
false

size or length


Returns the number of elements in a hash.

a = {'x'=>1,'y'=>2,'z'=>3}
puts "#{a.length}"
puts "#{a.size}"
Output
3
3

delete(key)


Deletes a key-value pair.

a = {'x'=>1,'y'=>2,'z'=>3}
a.delete('x')
puts "#{a}"
Output
{"y"=>2, "z"=>3}

each_key


Similar to each but iterate to the keys of a hash only.

a = {'x'=>1,'y'=>2,'z'=>3}
a.each_key do |i|
  puts "key is #{i} and value is #{a[i]}"
end
Output
key is x and value is 1
key is y and value is 2
key is z and value is 3

clear


Empties a hash

a = {'x'=>1,'y'=>2,'z'=>3}
a.clear
puts "#{a}"
Output
{}

invert


Inverts the keys and values of a hash. It means that in the new hash, previous keys are the new values and the previous values are the new keys.

a = {'x'=>1,'y'=>2,'z'=>3}
puts "#{a.invert}"
Output
{1=>"x", 2=>"y", 3=>"z"}

You must practice hard to become a good coder because coding is all about practice and problem solving.

There is no glory in practice but there is no glory without practice.


Ask Yours
Post Yours
Doubt? Ask question