ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.