📜  红宝石 |设置 flatten()函数

📅  最后修改于: 2022-05-13 01:54:26.468000             🧑  作者: Mango

红宝石 |设置 flatten()函数

flatten()是 Ruby 中的一个内置方法,它返回一个新集合,它是集合的副本,递归地展平每个包含集合。

示例 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


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


输出

Set: {1, 2, 4}

示例 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

输出

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