How to Merge Ruby Hashes

Oluwafisayo Oluwatayo Feb 15, 2024
  1. Simplified Method of Merging Ruby Hashes
  2. Detailed Approach to Merge Ruby Hashes
  3. Conclusion
How to Merge Ruby Hashes

Hashes are very similar to an array but differ in the sense that every hash usually has its own unique keys.

Using hashes is regarded as the best approach to keep data in a well-organized and easy-to-read structure, and also we can merge two Ruby hashes using their unique keys.

Let us now look at the various methods to merge Ruby hashes.

Simplified Method of Merging Ruby Hashes

This method is termed simplified because, concerning the second method, this is pretty straightforward, easy to work out and utilizes fewer codes.

We will create a new file, name it new.rb and add these codes:

a = { cheetah: { color: 'spotted' } }
b = { cheetah: { speed: '100mph' } }
a.merge(b) { |_key, a_val, b_val| a_val.merge b_val }

We have created the properties in the hash, each with its unique key. We want to merge the hashes to display the animal’s name, color, and speed.

method 1

Detailed Approach to Merge Ruby Hashes

This method requires us to add more details to our codes; in the example above, we only needed to merge the hashes by using the keys assigned to them, but here we include the property whose values we want to merge.

Create a new file, name it new.rb and type in these codes:

a = { cheetah: { color: 'spotted' } }
b = { cheetah: { speed: '100mph' } }
c = a[:cheetah].merge(b[:cheetah])
d = { cheetah: c }

After we have assigned values to each property with their unique keys, we create another hash to merge the two previous hashes; then, we create another hash to display the properties of the hash c.

method 2

In situations where we merge hashes with the same unique keys, Ruby will override the first hash with the second one.

To see that, let us create a new file, name it new.rb and type in these codes:

h1 = { 'a' => 'cheetah', 'b' => 'apple' }
h2 = { 'b' => 'mango', 'c' => 'lion' }
h1.merge!(h2)

merge overwrites

When we run this code, we see that the "apple" is not included in the merged hash. But we can get around this by tweaking our code a little bit.

Create a new file, name it new.rb and type in this code:

h1 = { 'a' => 'cheetah', 'b' => 'apple' }
h2 = { 'b' => 'mango', 'c' => 'lion' }
h1.merge(h2) { |_k, v1, v2| [v1, v2] }

merge overwrites workaround

Now we will see that instead of the property "b" displaying just the value of "mango", it will display both "apple" and "mango" under the property "b".

Conclusion

Hashes are a very suitable means to display data coherently, and assigning unique keys to each hash makes it easy to call the property to perform other functions or, in this case, to merge the hashes. Merging the hashes is made easier because of the unique keys assigned to the hashes.

Oluwafisayo Oluwatayo avatar Oluwafisayo Oluwatayo avatar

Fisayo is a tech expert and enthusiast who loves to solve problems, seek new challenges and aim to spread the knowledge of what she has learned across the globe.

LinkedIn

Related Article - Ruby Hash