Ruby | Hashes Basics (original) (raw)

Last Updated : 31 Jul, 2018

Hash is a data structure that maintains a set of objects which are termed as the keys and each key associates a value with it. In simple words, a hash is a collection of unique keys and their values. Hashes are sometimes called as associative arrays because it associates values with each of the keys but there is a difference between hashes and arrays. Arrays always use an integer value for indexing whereas hashes use the object. Hashes are also known as the maps because they map keys to values.Hash Literals or Creating Hashes: A hash is created using the hash literal which is a comma-separated list of key/value pairs and it always enclosed within curly braces {}. There are different ways to create a hash :

Fetching hash values: To fetch a hash value always put the required key within the square bracket [].Example:

Ruby `

Ruby program to demonstrate the creation

of hashes and fetching the hash values

#!/usr/bin/ruby

Creating a hash using new class method

without the default value

geeks = Hash.new

empty hash will return nothing on display

puts "#{geeks[4]}"

creating hash using new class method

providing default value

this could be written as

geeks = Hash.new "GFG"

geeks_default = Hash.new("GFG")

it will return GFG for every index of hash

puts "#{geeks_default[0]}" puts "#{geeks_default[7]}"

creating hash using {} braces

geeks_hash1 = {"DS" => 1, "Java" => 2}

fetching values of hash using []

puts geeks_hash1['DS']
puts geeks_hash1['Java']

`

Output:

GFG GFG 1 2

Modifying hashes in Ruby: Hash can be modified by adding or deleting a key value/pair in an already existing hash. Also, you can change the existing value of key in the hash.Example:

Ruby `

Ruby program to demonstrate the modifying of hash

#!/usr/bin/ruby

creating hash using {} braces

geeks_hash1 = {"DS" => 1, "Java" => 2}

puts "Before Modifying"

fetching values of hash using []

puts geeks_hash1['DS']
puts geeks_hash1['Java']

puts "\n"

puts "After Modifying"

modifying hash values

geeks_hash1["DS"] = 4 geeks_hash1["Java"] = 5

fetching values of hash using []

puts geeks_hash1['DS'] puts geeks_hash1['Java']

`

Output:

Before Modifying 1 2

After Modifying 4 5

Note: Whenever user provides two different values to the same key in a hash then the previous value of the key is overwritten by the latest value of the key. Also, the program will run successfully but will give a warning as shown in below example:

Ruby `

Ruby program to demonstrate the modifying hash

#!/usr/bin/ruby

creating hash using {} braces

providing two different values to key "DS"

geeks_hash1 = {"DS" => 1, "DS" => 4,"Java" => 2}

puts "Before Modifying"

fetching values of hash using []

puts geeks_hash1['DS']
puts geeks_hash1['Java']

puts "\n"

puts "After Modifying"

modifying hash values

geeks_hash1["DS"] = 4 geeks_hash1["Java"] = 5

fetching values of hash using []

puts geeks_hash1['DS'] puts geeks_hash1['Java']

`

Output:

Before Modifying 4 2

After Modifying 4 5 main.rb:7: warning: key "DS" is duplicated and overwritten on line 7