Home » Ruby programming

Hash.eql?() Method with Example in Ruby

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

Hash.eql?() Method

In this article, we will study about Hash.eql?() Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method with the help of its syntax and program code in the rest of the content.

Method description:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method works in a way that it carries out a comparison between two hashes. The first hash will be the hash object on which this method has been invoked and the second hash object will be the hash object which has been passed with the method. Hash.eql?() method returns a Boolean value and it will return true if both the hashes are equal and will return false if they both are not. The order of each hash is not compared. Two hashes are considered to be equal if they contain the same number of keys and there key values should be the same as the corresponding object in another hash object.

Syntax:

    Hash_object.eql?(hash_object1)

Argument(s) required:

This method only takes a single argument. The hash object passed is the object which will be compared with the first one.

Example 1:

=begin
  Ruby program to demonstrate .eql? operator
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}
hash2={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash.eql? implementation"

if(hash1.eql?(hash2))
	puts "hash1 is equal to hash2"
else
	puts "hash1 is not equal to hash2"
end

Output

Hash.eql? implementation
hash1 is equal to hash2

Explanation:

In the above code, you can simply observe that the method has returned true inside the if the condition that is because the message is printed as "hash1 is equal to hash2". This happened because hash1 is having all the elements which are present in hash2 or it is equal to hash2.

Example 2:

=begin
  Ruby program to demonstrate .eql? operator
=end	

hash1={"color"=>"Black","object"=>"phone","love"=>"mom","fruit"=>"Kiwi","vege"=>"potato"}
hash2={"object"=>"phone","fruit"=>"Kiwi","vege"=>"potato","love"=>"mom","color"=> "Black"}

puts "Hash.eql? implementation"

if(hash1.eql?(hash2))
	puts "hash1 is equal to hash2"
else
	puts "hash1 is not equal to hash2"
end

Output

Hash.eql? implementation
hash1 is equal to hash2

Explanation:

In the above code, you can observe that this method does not put emphasis upon the order of each hash object. It simply means that no matter in which order elements are stored in both the hashes, if the elements are the same then two hashes are equal otherwise not.



Comments and Discussions!

Load comments ↻





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