To check if a Set is empty in Ruby, you can use the empty?
method.
In this example,
set
and initialize it with some elements, or an empty Set if there are no elements.empty?
method on the Set set
to determine if it contains any elements.empty?
method returns true
; otherwise, it returns false
.require 'set'
set = Set.new # Or Set.new(['a', 'b', 'c']) for a non-empty Set
isEmpty = set.empty?
if isEmpty
puts 'The Set is empty'
else
puts 'The Set is not empty'
end
The Set is empty
In this example,
emptySet
.empty?
method on the Set emptySet
to determine if it contains any elements.empty?
method returns true
.require 'set'
emptySet = Set.new
isEmpty = emptySet.empty?
if isEmpty
puts 'The Set is empty'
else
puts 'The Set is not empty'
end
The Set is empty
In this tutorial, we learned How to check if a Set is Empty in Ruby language with well detailed examples.