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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
