Home »
Ruby Tutorial
Ruby Hash.replace() Method
By IncludeHelp Last updated : December 01, 2024
In this article, we will study about Hash.replace() Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method with the help of its syntax and program code in the rest of the content.
Description and Usage
This method is a public instance method that is defined in the ruby library especially for Hash class. The changes created by this method are permanent or non-temporary because this method is an example of destructive methods. This method works in a way that it will replace the content of the current hash with the contents of the hash on which the method has been invoked.
Syntax
Hash_object.replace(other_hash)
Parameters
This method only takes one parameter and that argument is nothing but the other hash whose content you want to store in the main hash.
Example 1
=begin
Ruby program to demonstrate replace method
=end
hsh = Hash.new()
hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"
puts "Hash contents before replace method are : #{hsh}"
hsh2 = {"name"=>"Hrithik","class"=>"bca","age"=>20}
puts "Hash replace implementation"
puts "#{hsh.replace(hsh2)}"
puts "Hash contents after replace method are : #{hsh}"
Output
Hash contents before replace method are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
Hash replace implementation
{"name"=>"Hrithik", "class"=>"bca", "age"=>20}
Hash contents after replace method are : {"name"=>"Hrithik", "class"=>"bca", "age"=>20}
Explanation
In the above code, you can observe that we can replace the contents of one hash with the contents of another hash object with the help of the Hash.replace() method. You may observe that you can pass a hash object with the method at the time of its invocation. The method is creating permanent change on the hash object.
Example 2
=begin
Ruby program to demonstrate replace method
=end
hsh = Hash.new()
hsh["color"] = "Black"
hsh["age"] = 20
hsh["school"] = "Angels' Academy Haridwar"
hsh["college"] = "Graphic Era University"
puts "Hash replace implementation"
puts "Hash contents before replace method are : #{hsh}"
puts "#{hsh.replace({"name"=>"Hrithik","class"=>"bca","age"=>20})}"
puts "Hash contents after replace method are : #{hsh}"
Output
Hash replace implementation
Hash contents before replace method are : {"color"=>"Black", "age"=>20, "school"=>"Angels' Academy Haridwar", "college"=>"Graphic Era University"}
{"name"=>"Hrithik", "class"=>"bca", "age"=>20}
Hash contents after replace method are : {"name"=>"Hrithik", "class"=>"bca", "age"=>20}
Explanation
In the above code, you can observe that we can replace the contents of one hash with the contents of another hash object with the help of the Hash.replace() method. You may observe that you can pass multiple keys and values with the method at the time of its invocation. The method is creating permanent change on the hash object.