The addition of two sets in Ruby results in the set1
and set2
is a new set containing elements of set1
and set2
. This is made possible using the plus operator, +
.
Note: Sets do not accommodate duplicate values or elements.
set1 + set2
set1
and set2
: These are the sets we want to get the union of.
It returns a new set with elements of set1
and set2
.
# 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 union Union1 = EvenNumbers + NumbersToTen Union2 = Names + Workers # print out intersects puts "#{Union1.to_a}" puts "#{Union2.to_a}"
set
instance.+
operator to take the union of set
instances.RELATED TAGS
CONTRIBUTOR
View all Courses