Home »
Ruby »
Ruby Programs
Ruby program to compare two hash collections using '==' operator
Ruby Example: Write a program to compare two hash collections using '==' operator.
Submitted by Nidhi, on February 14, 2022
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 »