A set is a collection of unordered elements without duplicates. This means that a set is a collection of unique elements. In Ruby, two sets are said to be equal when they have similar or common elements. It means both sets should have identical elements.
set1 == set2
set1
and set2
: We will use these two sets to check if they are equal or not.
The set has a return value which is either true
or false
. If the sets are equal, then a Boolean, true
is returned, else a false
is returned.
# 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])RandomNumbers = Set.new([6, 8, 10, 4, 2])Names = Set.new(["Amaka", "Titi", "Chioma"])Workers = Set.new(["Amaka", "Chioma", "Titi"])# check equalityputs EvenNumbers == NumbersToTen # falseputs RandomNumbers == EvenNumbers # trueputs Names == Workers # true
require
to get the set class.==
operator. We print the results to our system console.