Open In App

Ruby | Set flatten() function

The flatten() is an inbuilt method in Ruby returns a new set that is a copy of the set, flattening each containing set recursively.

Syntax: s1.flatten()



Parameters: The function does not takes any parameter.

Return Value: It returns a boolean value. It returns true if the set is empty or it returns false.



Example 1




# Ruby program to illustrate the flatten method
  
# requires the set
require "set"
  
s1 = Set[1, 2, 4, 4]
  
# flatten method used
s2 = s1.flatten()
  
# Prints s2
puts s2

Output

Set: {1, 2, 4}

Example 2




# Ruby program to illustrate the flatten method
  
# requires the set
require "set"
  
s1 = Set[16, 8, 3, 5, 2]
  
# flatten method used
s2 = s1.flatten()
  
# Prints s2
puts s2

Output

Set: {16, 8, 3, 5, 2}

 

Article Tags :