Q:

Ruby program to check a hash collection is empty or not

belongs to collection: Ruby Hashes Programs

0

In this program, we will create a hash collection. Then we will check a collection is empty or not using empty?() method.

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 check a hash collection is empty or not is given below. The given program is compiled and executed on Windows 10 Operating System successfully.

# Ruby program to check a hash collection 
# is empty or not

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

if hash1.empty?() == true
    puts "hash1 collection is empty";
else
    puts "hash1 collection is not empty";
end

if hash2.empty?() == true
    puts "hash2 collection is empty";
else
    puts "hash2 collection is not empty";
end

Output:

hash1 collection is not empty
hash2 collection is empty

Explanation:

In the above program, we created two hash collections. Then we checked created collections are empty or not using empty?() method 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 check a value is exist in the hash... >>
<< Ruby program to delete the item from the hash coll...