Ruby Set union() method
In ruby programming language, union() set methods are used to merge an enumerable object into set. The syntax of this method as follows.
set.union(enum) -> new set
This method are accept an enumerable object and returning a new set. The resultant set contains distinct objects.
Ruby union method example
require "set"
# Set s1
s1 = Set[10,20,['a', 'b']]
# Set s2
s2 = Set[40,30]
# Set s3
# union of s1 and s2 set
s3 = s1.union(s2)
# union of s1 set and enumerable object
s4 = s2.union([40,60])
# Display result
print("\ns1 : ",s1.to_a)
print("\ns2 : ",s2.to_a)
print("\ns3 : ",s3.to_a)
print("\ns4 : ",s4.to_a)

s1 : [10, 20, ["a", "b"]]
s2 : [40, 30]
s3 : [10, 20, ["a", "b"], 40, 30]
s4 : [40, 30, 60]
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment