Q:

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

belongs to collection: Ruby Hashes Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 hash1hash2hash3. Then we compared created hash collections using the "==" operator and printed the appropriate message.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ruby Hashes Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to get value from the hash collection... >>
<< Ruby program to get keys from a hash collection...