Home » Ruby programming

Hash.update(other_hash) Method with Example in Ruby

Ruby Hash.update(other_hash) Method: Here, we are going to learn about the Hash.update(other_hash) Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on March 14, 2020

Hash.update(other_hash) Method

In this article, we will study about Hash.update(other_hash) 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.

Method description:

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. This method is one of the examples of destructive methods where the changes created by the methods are non-temporary or permanent.

Syntax:

    Hash_object.update(other_hash)

Argument(s) required:

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.update(other_hash) method
=end	

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

puts "Hash.update implementation:"

hash3 = hsh.update(hsh1)

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

puts "Original hash : #{hsh}"

Output

Hash.update implementation:
The keys present in the new hash are: {"colors"=>"red", "letters"=>"a", "Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo", "home"=>"shivalik nagar", "city"=>"Haridwar", "state"=>"Uttrakhand"}
Original hash : {"colors"=>"red", "letters"=>"a", "Fruit"=>"Grapes", "anything"=>"red", "sweet"=>"ladoo", "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.update() method. You can see that the new hash is containing the keys and values of both the hashes. This method is creating changes in the original hash because this method is an example of destructive methods where the changes created by the method are permanent.

Example 2:

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

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

puts "Hash.update implementation:"

hash3 = hsh.update(hsh1)

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

puts "Original hash : #{hsh}"

Output

Hash.update implementation:
The keys present in the new hash are: {"home"=>"shivalik nagar", "city"=>"Haridwar", "state"=>"Uttrakhand"}
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.update() method. You can see that the new hash is containing non-duplicate keys and values even though there is a repetition of keys in both the hashes but the new hash is storing them only for once. This method is creating changes in the original hash because this method is an example of destructive methods where the changes created by the method are permanent.



Comments and Discussions!

Load comments ↻





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