红宝石 |设置合并()函数
merge()是 Ruby 中的一个内置方法,在将传递的对象合并为一个集合后返回新集合。
Syntax: s1_name.merge(object)
Parameters: The function accepts a mandatory parameter enumerable object which is to be merged for.
Return Value: It returns the new set after merging.
示例 1 :
# Ruby program to illustrate
# the merge method
# requires the set
require "set"
s1 = Set[1, 2, 3]
s2 = Set[6]
# merge method used
puts s1.merge(s2)
输出:
Set: {1, 2, 3, 6}
示例 2 :
# Ruby program to illustrate
# the merge method
# requires the set
require "set"
s1 = Set[1, 2, 3]
# merge method used
puts s1.merge([9, 10, 12])
输出:
Set: {1, 2, 3, 9, 10, 12}