红宝石 |设置 flatten()函数
flatten()是 Ruby 中的一个内置方法,它返回一个新集合,它是集合的副本,递归地展平每个包含集合。
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.
示例 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}