📜  红宝石 |设置 compare_by_identity?函数

📅  最后修改于: 2022-05-13 01:55:47.062000             🧑  作者: Mango

红宝石 |设置 compare_by_identity?函数

compare_by_identity?()是 Ruby 中的一个内置方法,如果集合将按其身份比较其元素,则返回 true。

示例 1

#Ruby program to illustrate the
#compare_by_identity ? method
  
#requires the set
require "set"
  
    s1
    = Set[]
  
      puts s1.compare_by_identity
    ? ()
  
#Add 10 to it
              s1
          << 10
  
             puts s1.compare_by_identity
    ? ()
  
              s1
          << 'a'
  
             puts s1.compare_by_identity
    ? ()

输出

false
false
false

示例 2

#Ruby program to illustrate the
#compare_by_identity ? method
  
#requires the set
require "set"
  
    s1
    = Set[1, 2, 3]
  
      puts s1.compare_by_identity
    ? ()
  
#Add 12 to it
              s1
          << 12
  
             puts s1.compare_by_identity
    ? ()

输出

false
false

参考:https://devdocs.io/ruby~2.5/set#method-i-compare_by_identity-3F