×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby Hash.merge(other_hash){block} Method

By IncludeHelp Last updated : December 01, 2024

In this article, we will study about Hash.merge(other_hash){block} Method. The working of the method can’t be assumed because of it’s quite a different name. Let us read its definition and understand its implementation with the help of syntax and program codes.

Description and Usage

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. If you are specifying a block then the value of duplicate keys is known to bypass the value into the block. This method is one of the examples of non-destructive methods where the changes created by the methods are temporary or not permanent.

Syntax

Hash_object.merge(other_hash){|key,oldval,newval|}

Parameters

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

Example 1

=begin
  Ruby program to demonstrate 
  Hash.merge(other_hash){block} method
=end	

hsh = {"colors"  => "red","city"=>"Nainital", "Fruit" => "Grapes", "anything"=>"red","sweet"=>"ladoo"}
hsh1 = {"home" => "shivalik nagar", "city"=>"Haridwar","state"=>"Uttrakhand"}

puts "Hash.merge{block} implementation:"

hash3 = hsh.merge(hsh1){|key,oldval,newval| oldval+newval}

puts "The keys present in the new hash are: #{hash3}"

puts "Original hash : #{hsh}"

Output

Hash.merge{block} implementation:
The keys present in the new hash are: {"colors"=>"red", "city"=>"NainitalHaridwar", "Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo", "home"=>"shivalik nagar", "state"=>"Uttrakhand"}
Original hash : {"colors"=>"red", "city"=>"Nainital", "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 observe that when the duplicate value came, the block has been executed and it has done certain manipulation upon the value of keys. This method is not creating changes in the original hash because this method is an example of non-destructive methods where the changes created by the method are not permanent.

Example 2

=begin
  Ruby program to demonstrate 
  Hash.merge(other_hash) method
=end	

hsh = {"home" => "shivalik nagar", "city"=>"Haridwar","state"=>"Uttrakhand"}
hsh1 = {"home" => "shivalik nagar", "city"=>"Haridwar","state"=>"Uttrakhand"}

puts "Hash.merge implementation:"

hash3 = hsh.merge(hsh1){|key,oldval,newval| oldval+newval}

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: {"home"=>"shivalik nagarshivalik nagar", "city"=>"HaridwarHaridwar", "state"=>"UttrakhandUttrakhand"}
Original hash : {"home"=>"shivalik nagar", "city"=>"Haridwar", "state"=>"Uttrakhand"}

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 observe that when the duplicate value came, the block has been executed and it has done certain manipulation upon the value of keys. This method is not creating changes in the original hash because this method is an example of non-destructive methods where the changes created by the method are not permanent.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.