ADVERTISEMENT
ADVERTISEMENT

How to add elements to a Hash in Ruby?

Here, we are going to learn how to add elements to a hash in Ruby programming language using different methods?
Submitted by Hrithik Chandra Prasad, on May 07, 2020

Before going through the ways to add elements to the hash instances, let us understand what could be called as a hash element. So, Hash is the collection of keys and their values. For example,

    Hash1 = {"Color" => "Red"} # is a hash object as it has a key value pair.

Now, let us understand the different ways through which we can add elements in the hash object.

Method 1: Use Hash.store() method

This method is a public instance method that is defined in the ruby library especially for the Hash class. This method works in a way that it stores or assigns the given value into the key with which the method has been invoked. This method takes two parameters, one is the key and another one is the value of that particular key.

This method does bring change in the actual hash because this method belongs to the category of destructive methods.

Syntax:

    Hash_object.store(key,value)

Parameter(s) required:

This method takes two parameters, one is the key and another one is the value of that particular key.

Program:

=begin
  Ruby program to demonstrate store method
=end	

hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash store implementation"
puts "Enter the key:"
ky = gets.chomp

puts "Enter the value:"
val = gets.chomp

hsh = hash1.store(ky,val)

puts  "Key updated is #{hsh}"
puts "Self hash object : #{hash1}"

Output

Hash store implementation
Enter the key:
 country
Enter the value:
 India
Key updated is India
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", 
"fruit"=>"Kiwi", "vege"=>"potato", "country"=>"India"}

Explanation:

In the above code, you can observe that we are storing values in the hash object with the help of the Hash.store() method. You can see how we can update the value of a particular key in the hash object with the help of this method. This method is creating changes in the actual hash object because this method is one of the examples of destructive methods.

Method 2: With the help of Hash.merge() method

You can add elements in a particular hash with the help of another hash as well. You only have to store elements that you want to add in a different hash and apply merge operation.

This method is a Public instance method and belongs to the Hash class which lives inside the library of Ruby language. This method works in a way that it returns a new hash object which contains the keys and values of self hash as well as another hash. If both the hashes are containing the same keys and values then the new hash will not contain duplicate keys and values or you can say that each key and value will be stored only for once. This method is one of the examples of non-destructive methods where the changes created by the methods are temporary or non-permanent.

Syntax:

    Hash_object.merge(other_hash)

Parameter(s) required:

This method only takes one parameter and that argument is nothing but another hash instance you want to merge.

Program:

=begin
  Ruby program to demonstrate 
  Hash.merge(other_hash) method
=end	
hsh={"colors"=>"red","letters"=>"a","Fruit"=>"Grapes","anything"=>"red","sweet"=>"ladoo"}

hsh1={"home"=>"shivaliknagar","city"=>"Haridwar","state"=>"Uttrakhand"}

puts "Hash.merge implementation:"
hash3 = hsh.merge(hsh1)
puts "The keys present in the new hash are: #{hash3}"
puts "Original hash : #{hsh}"

Output

Hash.merge implementation:
The keys present in the new hash are: {"colors"=>"red", "letters"=>"a", 
"Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo", "home"=>"shivaliknagar", 
"city"=>"Haridwar", "state"=>"Uttrakhand"}
Original hash : {"colors"=>"red", "letters"=>"a", "Fruit"=>"Grapes", 
"anything"=>"red", "sweet"=>"ladoo"}

Explanation:

In the above code, you can observe that you can merge with another hash with the help of the Hash.merge() method. You can see that the new hash is containing the keys and values of both the hashes. This method is not creating any changes in the original hash because this method is an example of non-destructive methods where the changes created by the method are non-permanent.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.