To check if a specific item is present in a Set in Ruby, you can use the include?
method.
In this example,
my_set
containing elements of a specific type.include?
method of the Set to check if a specific item is present.include?
method returns true
if the item is present in the Set, otherwise it returns false
.require 'set'
my_set = Set.new(["apple", "banana", "orange", "grape", "melon"])
# Check if item is present
item = "banana"
is_present = my_set.include?(item)
# Print result
puts "Is #{item} present in the Set? #{is_present}"
Is banana present in the Set? true
In this example,
my_set
containing elements of a specific type.include?
method of the Set to check if a specific item is not present.include?
method returns false
if the item is not present in the Set.require 'set'
my_set = Set.new(["apple", "banana", "orange", "grape", "melon"])
# Check if item is not present
item = "peach"
is_not_present = !my_set.include?(item)
# Print result
puts "Is #{item} not present in the Set? #{is_not_present}"
Is peach not present in the Set? true
In this tutorial, we learned How to check if a Specific Item is present in the Set in Ruby language with well detailed examples.