×

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.update(other_hash) Method

By IncludeHelp Last updated : December 01, 2024

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.

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. 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)

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.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.