Ruby | Hashes Basics
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 :
- Using new class method: new class method will create an empty hash means there will be no default value for the created hash.
Syntax:
hash_variable = Hash.new
Example:
geeks = Hash.new
This will create an empty hash geeks. You can also provide the default value to geeks in two ways as follows:
geeks = Hash.new("GFG")
or
geeks = Hash.new "GFG"
Here GFG is the default value. Whenever the key or value doesn’t exist in above hash then accessing the hash will return the default value “GFG”. To provide key/value pairs you have to modify the hash values which is discussed below in this article.
- Using {} braces: In this hash variable is followed by = and curly braces {}. Between curly braces {}, the key/value pairs are created.
Syntax:
hash_variable = {"key1" => value1, "key2" => value2}
Fetching hash values: To fetch a hash value always put the required key within the square bracket [].
Example:
geeks = Hash . new
puts "#{geeks[4]}"
geeks_default = Hash . new ( "GFG" )
puts "#{geeks_default[0]}"
puts "#{geeks_default[7]}"
geeks_hash1 = { "DS" => 1 , "Java" => 2 }
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:
geeks_hash1 = { "DS" => 1 , "Java" => 2 }
puts "Before Modifying"
puts geeks_hash1[ 'DS' ]
puts geeks_hash1[ 'Java' ]
puts "\n"
puts "After Modifying"
geeks_hash1[ "DS" ] = 4
geeks_hash1[ "Java" ] = 5
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:
geeks_hash1 = { "DS" => 1 , "DS" => 4 , "Java" => 2 }
puts "Before Modifying"
puts geeks_hash1[ 'DS' ]
puts geeks_hash1[ 'Java' ]
puts "\n"
puts "After Modifying"
geeks_hash1[ "DS" ] = 4
geeks_hash1[ "Java" ] = 5
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...