In Ruby, the intersect?()
method is used to check to see if two sets have an intersection. If they do, then this method returns a true
value. Else, it returns false
.
set1.intersect?(set2)
The set1
and set2
are sets we want to check if they have any intersection (common elements).
It returns a boolean value, true
, if there is an intersection. Otherwise, it returns false
.
# require the set classrequire "set"# create some setsEvenNumbers = Set.new([2, 4, 6, 8, 10])NumbersToTen = Set.new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])Names = Set.new(["Amaka", "Titi"])Workers = Set.new(["Amaka", "Chioma", "Titi"])EmptySet = Set.new([])# check if intersections existsputs EvenNumbers.intersect?(NumbersToTen) # trueputs Names.intersect?(Workers) # trueputs EvenNumbers.intersect?(EmptySet) # falseputs Names.intersect?(EmptySet) # false
set
class.intersect?()
method to check to see if some of the sets have intersections. Next, we print the returned result to the console.