The following strange behavior happened in ruby ββ1.8.6, in 1.8.7 it works correctly. Does anyone know what this would cause?
h = {} key_1 = {1 => 2} key_2 = {1 => 2} h[key_1] = 3 p key_1 == key_2 # => true p h.has_key?(key_2) # => expect true, get false, wtf?
I thought this would be caused by the implementation of the hash method in the Hash class.
p [key_1.hash, key_2.hash]
but even if I override the Hash hash method
class Hash def hash return self.keys.hash + self.values.hash end end p [key_1.hash, key_2.hash] # => [8,8] (same p h.has_key?(key_2) # => false
link to the code link to the online results of the Ruby 1.8.6 interpreter: http://codepad.org/7nCYMP4w
source share