不同之处在于Ruby中的内置方法,该方法通过复制集合,删除出现在给定可枚举对象中的每个元素来返回建立的新集合。
用法: s1.name – s2.name
参数:该函数不带任何参数。
返回值:它会通过复制集合,并删除出现在给定可枚举对象中的每个元素来返回建立的新集合。
例子1:
# Ruby program to illustrate the difference method  
   
# requires the set  
require "set"
   
s1 = Set[1, 2, 4] 
s2 = Set[1, 2, 3]  
   
# difference method used 
s3 = s1.difference(s2) 
   
# Prints the s3  
puts s3输出:
Set: {4}
例子2:
# Ruby program to illustrate the difference method  
   
# requires the set  
require "set"
   
s1 = Set[1, 2, 4] 
s2 = Set[6, 5, 3]  
   
# difference method used 
s3 = s1.difference(s2) 
    
# Prints the s3  
puts s3输出:
Set: {1, 2, 4}
相关用法
- Ruby Set add?用法及代码示例
- Ruby StringScanner pos用法及代码示例
- Ruby SizedQueue enq()用法及代码示例
- Ruby Array map()用法及代码示例
- Ruby Array one?()用法及代码示例
- Ruby SizedQueue deq()用法及代码示例
- Ruby Set subtract()用法及代码示例
- Ruby StringScanner pos=用法及代码示例
- Ruby Time now()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Ruby | Set difference() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
