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 set require "set" # create some sets EvenNumbers = 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 intersection puts EvenNumbers < NumbersToTen # true puts Workers < Names # false
require
to get the set class.<
operator to check if some sets were subsets for another.RELATED TAGS
CONTRIBUTOR
View all Courses