Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Set flatten() function

  • Last Updated : 04 Dec, 2020

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




# 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




# 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}

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :