红宝石 |设置减法()函数
subtract()是 Ruby 中的一个内置方法,它在删除传递的枚举中出现的所有对象后返回集合。
Syntax: s1_name.subtract(enum)
Parameters: The function takes an object enum whose elements are deleted from the set.
Return Value: It returns self object after removing all elements are removed of enum from set.
示例 1 :
# Ruby program to illustrate
# the subtract() method
# requires the set
require "set"
s1 = Set[2, 12, 78, 10, 87, 98]
s2 = Set[2, 10, 87]
# subtract method used
puts s1.subtract(s2)
输出:
Set: {12, 78, 98}
示例 2 :
# Ruby program to illustrate
# the subtract() method
# requires the set
require "set"
s1 = Set[4, 5, 6, 7, 10]
# subtract method used
puts s1.subtract([5, 7])
输出:
Set: {4, 6, 10}