×

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 program to compare two hash collections using '==' operator

Last Updated : December 15, 2025

Problem Solution

In this program, we will create three hash collections. Then we will compare created collections using the "==" operator.

Program/Source Code

The source code to compare two hash collections using the "==" operator is given below. The given program is compiled and executed on Windows 10 Operating System successfully.

# Ruby program to compare two hash collections 
# using '==' operator

hash1 = {"101" => "Amit", "102" => "Arun", "103" => "Sumit"};
hash2 = {"101" => "Amit", "102" => "Ankit", "103" => "Sumit"};
hash3 = {"101" => "Amit", "102" => "Arun", "103" => "Sumit"};

if hash1==hash2
    print "hash1 and hash2 are equal\n";
else
    print "hash1 and hash2 are not equal\n";
end

if hash1==hash3
    print "hash1 and hash3 are equal\n";
else
    print "hash1 and hash3 are not equal\n";
end

Output

hash1 and hash2 are not equal
hash1 and hash3 are equal

Explanation

In the above program, we created 3 hash collections hash1, hash2, hash3. Then we compared created hash collections using the "==" operator and printed the appropriate message.

Ruby Hashes Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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