Ruby - Hashes



A Hash is a collection of key-value pairs like this: "employee" = > "salary". It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index.

The order in which you traverse a hash by either key or value may seem arbitrary and will generally not be in the insertion order. If you attempt to access a hash with a key that does not exist, the method will return nil.

Creating Hashes

As with arrays, there is a variety of ways to create hashes. You can create an empty hash with the new class method −

months = Hash.new

You can also use new to create a hash with a default value, which is otherwise just nil

months = Hash.new( "month" )

or

months = Hash.new "month"

Example

When you access any key in a hash that has a default value, if the key or value doesn't exist, accessing the hash will return the default value −

months = Hash.new( "month" )

puts "#{months[0]}"
puts "#{months[72]}"

Output

This will produce the following result −

month
month

Example

H = Hash["a" => 100, "b" => 200]

puts "#{H['a']}"
puts "#{H['b']}"

Output

This will produce the following result −

100
200

You can use any Ruby object as a key or value, even an array, so the following example is a valid one −

[1,"jan"] => "January"

Hash Built-in Methods

We need to have an instance of Hash object to call a Hash method. As we have seen, following is the way to create an instance of Hash object −

Hash[[key =>|, value]* ] or

Hash.new [or] Hash.new(obj) [or]
Hash.new { |hash, key| block }

Example

This will return a new hash populated with the given objects. Now using the created object, we can call any available instance methods. For example −

$, = ", "
months = Hash.new( "month" )
months = {"1" => "January", "2" => "February"}

keys = months.keys
puts "#{keys}"

Output

This will produce the following result −

["1", "2"]
Advertisements