Home » Ruby programming

Hash < other_hash (< operator) in Ruby

Ruby Hash < other_hash (< operator): In this tutorial, we are going to learn about Hash < other_hash (< operator) with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 15, 2020

In this article, we will see about the implementation of "<" operator. The working is pretty clear with the help of its name. It is not as simple as it seems. We will figure it out in the content of this article. We will understand it with the help of syntaxes and demonstrating program codes.

Method description:

This method is a public instance method that is defined in Ruby's library especially for Hash class. This method works in a way that it carries out a comparison between two different hashes and returns a Boolean value. The method returns true when the hash is a subset of another hash and returns false if it is not the subset of another Hash instance. Being a subset simply means to have all those elements which are present in another Hash object.

Syntax:

    Hash < Hash_object -> true or false

Parameter(s) required:

This method does not require any argument.

Example 1:

=begin
  Ruby program to demonstrate new method
=end	

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

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

if(hash1<hash2)
	puts "hash1 is a subset of hash2"
else
	puts "hash1 is not a subset of hash2"
end

Output

hash1 is a subset of 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 the subset of hash2". This happened because hash1 is having all the elements which are present in hash2. This is the simple meaning of the subset.

Example 2:

=begin
  Ruby program to demonstrate < operator
=end	

hash1 ={"color"=> "Black", "object"=>"phone", "love"=>"mom","fruit"=>"Kiwi","animal"=>"cow"}

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

if(hash1<hash2)
	puts "hash1 is a subset of hash2"
else
	puts "hash1 is not a subset of hash2"
end

Output

hash1 is not a subset of hash2

Explanation:

In the above code, you can simply observe that the method has returned false inside the if the condition that is because the message is printed as "hash1 is not a subset of hash2". This happened because hash1 is not having all the elements which are present in hash2. This is the simple meaning of the subset.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.