Ruby Set flatten method
In ruby programming, flatten() Set methods are used to copy set object. This method are are flattening each containing set recursively and return resultant newly set.
set.flatten() -> new set
Example of Set flatten method
Basically this method are flattened a given set elements and copy non enumerable objects and share enumerable objects. See this example.
require "set"
# Set a1
a1 = Set[10,20,['a', 'b']]
# Set a2
a2 = Set[40,a1,30]
# Set a3
a3 = Set[50,a2]
# Copy set
result = a3.flatten()
# Display result
print("\na1 : ",a1.to_a)
print("\na2 : ",a2.to_a)
print("\na3 : ",a3.to_a)
print("\nresult : ",result.to_a)

a1 : [10, 20, ["a", "b"]]
a2 : [40, #<Set: {10, 20, ["a", "b"]}>, 30]
a3 : [50, #<Set: {40, #<Set: {10, 20, ["a", "b"]}>, 30}>]
result : [50, 40, 10, 20, ["a", "b"], 30]
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