Tag: 相等

在比较密钥时,Ruby的Hash使用哪种相等性测试?

我有一个围绕一些对象的包装类,我想用它作为哈希中的键。 包装和解包对象应映射到同一个键。 一个简单的例子是: class A attr_reader 😡 def initialize(inner) @inner=inner end def x; @inner.x; end def ==(other) @inner.x==other.x end end a = A.new(o) #o is just any object that allows ox b = A.new(o) h = {a=>5} ph[a] #5 ph[b] #nil, should be 5 ph[o] #nil, should be 5 我试过==,===,eq? 哈希都无济于事。