When we say a Hash is empty, what we mean is that the Hash has no entries.
We can use the empty?
method to check if a Hash has no entries. This method returns a Boolean, which indicates whether the Hash is empty or not.
The syntax is given below:
hash.empty?
hash
: This is the hash we want to check to see if it is empty or not.
The value returned is a Boolean value. If the Hash is empty, then True
is returned. If a Hash is not empty, then False
is returned.
# create hashesh1 = {one: 1, two: 2, three: 3}h2 = {}h3 = {name: "okwudili", stack: "ruby"}h4 = {"foo": 0, "bar": 1}h5 = Hash[]# check if empty and# print resultsputs h1.empty? # falseputs h2.empty? # trueputs h3.empty? # falseputs h4.empty? # falseputs h5.empty? # true