In Ruby, you can find the union of two sets using the | operator or the union method provided by the Set class.
In this example,
set1
and set2
.set1
and set2
.require 'set'
set1 = Set.new([1, 2, 3])
set2 = Set.new([3, 4, 5])
union_set = set1 | set2
puts union_set.inspect
#<Set: {1, 2, 3, 4, 5}>
In this example,
set1
and set2
.union
method on set1
with set2
as an argument.require 'set'
set1 = Set.new([1, 2, 3])
set2 = Set.new([3, 4, 5])
union_set = set1.union(set2)
puts union_set.inspect
#<Set: {1, 2, 3, 4, 5}>
In this tutorial, we learned How to find Union of Two Sets in Ruby language with well detailed examples.