In Ruby, the disjoint?()
method is used on sets. A set is a collection of unordered unique elements. When this method is called, it returns a Boolean value. If two sets have no element in common then we say they are disjoint. A disjoint is the opposite of an intersection.
set1.disjoint?(set2)
set1
and set2
: We want to check if set1 and set2 are disjoint.
The value returned is a Boolean. true
is returned if the two sets have no element in common, that is they are disjoint. Otherwise, false
is returned.
# require set classrequire "set"# create some setsset1 = Set.new([1, 2, 3])set2 = Set.new([4..5])set3 = Set.new([3, 4])set4 = Set.new([4, 5])# check if disjoint setsputs set1.disjoint? set2 # trueputs set1.disjoint? set3 # falseputs set1.disjoint? set4 # true
set
class.disjoint?
method and print the results to the console.