The contents of a certain hash can be replaced in Ruby by using the replace()
method. It replaces the entire contents of a hash with the contents of another hash.
hash.replace(another_hash)
hash
: This is the hash whose contents we want to replace with another hash.
another_hash
: This is the hash that will replace the contents of the hash hash
.
The hash
is still returned but its original contents are replaced by contents of hash another_hash
.
# create hashesh1 = {one: 1, two: 2, three: 3}h2 = {name: "okwudili", stack: "ruby"}h3 = {"foo": 0, "bar": 1}h4 = {M:"Mango", A: "Apple", B: "Banana"}# replace hash contents# and print resultsputs h1.replace(h2)puts h2.replace(h3)puts h3.replace(h4)puts h4.replace(h1)
replace()
method to replace hash contents with another.