In Ruby, we use the less-than operator <
to determine if a particular set is a subset of another. If set1
is a subset of set2
, then all the elements of set1
are present in set2
. Remember that a set is a collection of unordered values without duplicates.
set1 < set2
set1
: This is the set for which we want to check if it is the subset of set2
.
set2
: This is the set for which we want to check if set1
is its subset.
A Boolean value is returned, which tells that set1
is a subset of sub2
. If it is a subset then a true
is returned, otherwise false
.
# require setrequire "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"])# get the intersectionputs EvenNumbers < NumbersToTen # trueputs Workers < Names # false
require
to get the set class.<
operator to check if some sets were subsets for another.