Home »
Ruby »
Ruby Programs
Ruby program to check a hash collection is empty or not
Ruby Example: Write a program to check a hash collection is empty or not.
Submitted by Nidhi, on February 16, 2022
Problem Solution:
In this program, we will create a hash collection. Then we will check a collection is empty or not using empty?() method.
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.
Ruby Hashes Programs »