compare_by_identity()是Ruby中的一个内置方法,使该集合通过其标识比较其元素并返回self。 Set的所有子类可能不支持此方法。
用法:s1.name.compare_by_identity()
参数:该函数不带任何参数。
返回值:比较元素后返回self
例子1:
#Ruby program to illustrate the compare_by_identity() method 
  
#requires the set 
require "set"
  
    s1 
    = Set[] 
  
#Add 10 to it 
      s1 
      << 10 
  
#Returns self after comparing internally 
         puts s1.compare_by_identity() 
  
             s1 
      << 'a'
  
#Returns self after comparing internally 
         puts s1.compare_by_identity()输出:
Set:{10}
Set:{10, "a"}
例子2:
#Ruby program to illustrate the clear method 
  
#requires the set 
require "set"
  
    s1 
    = Set[] 
  
#Prints s1 
      puts s1 
  
#Add 10 to it 
          s1 
      << 10 
  
         puts s1 
  
#Clears the set and returns self 
#which is printed 
             puts s1.clear输出:
Set:{}
Set:{10}
Set:{}
参考:https://devdocs.io/ruby~2.5/set#method-i-compare_by_identity
相关用法
- Ruby Set add?用法及代码示例
- Ruby Struct each()用法及代码示例
- Ruby Set subtract()用法及代码示例
- Scala Symbol =~用法及代码示例
- Ruby Set subset?用法及代码示例
- Ruby Array map()用法及代码示例
- Ruby Integer odd?用法及代码示例
- Ruby Integer div()用法及代码示例
- Ruby Hash eql?用法及代码示例
- Ruby Range eql?()用法及代码示例
- Scala Struct ==用法及代码示例
- Ruby Hash dig()用法及代码示例
- Ruby Set superset?用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 Ruby | Set compare_by_identity() function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
