We can merge two hashes using the merge()
method.
When using the merge()
method:
hash.merge(other_hash)
other_hash
: This is the hash we want to merge with the hash
. They could be one or more.
This method returns a new hash object formed by merging other_hash
into hash hash
.
# create some hashesh1 = {one: 1, two: 2, three: 3}h2 = {two:22, three: 33}h3 = {"foo": 0, "bar": 1}h4 = {"foo": 1}h4 = {M:"Mango", A: "Apple", B: "Banana"}h5 = {C: "Cabbage", P: "Pineapple"}# merge hashesputs h1.merge(h2)puts h3.merge(h4)puts h4.merge(h5)