subtract()是Ruby中的一个内置方法,它会在删除所有通过的枚举中出现的对象后返回该集合。
用法: s1_name.subtract(enum)
参数:该函数接受一个对象枚举,其元素将从集合中删除。
返回值:从集合中删除所有枚举后,它返回self对象。
例子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}
相关用法
- Ruby Set add?用法及代码示例
- Ruby Set compare_by_identity?用法及代码示例
- Ruby Set delete()用法及代码示例
- Ruby Set compare_by_identity()用法及代码示例
- Ruby Set reset()用法及代码示例
- Scala Complex +用法及代码示例
- Scala Complex ==用法及代码示例
- Ruby Set disjoint?用法及代码示例
- Ruby Set replace()用法及代码示例
- Ruby Set flatten()用法及代码示例
- Ruby Set difference()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Ruby | Set subtract() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
