From: daniel@...42.com Date: 2020-05-13T17:22:36+00:00 Subject: [ruby-core:98324] [Ruby master Bug#16850] Object#hash doesn't behave as documented Issue #16850 has been updated by Dan0042 (Daniel DeLorme). Thanks for the insight into bypass vs deoptimization. Of course if you override #hash and #eql? to return true for previously false cases then it wouldn't work, but anyway I don't see much use or reason for doing this on Integer or String. The added documentation is plenty good enough. ---------------------------------------- Bug #16850: Object#hash doesn't behave as documented https://bugs.ruby-lang.org/issues/16850#change-85562 * Author: ana06 (Ana Maria Martinez Gomez) * Status: Closed * Priority: Normal * ruby -v: master * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- From [Ruby 2.7 Object class documentation](https://ruby-doc.org/core-2.7.0/Object.html#method-i-hash): >The hash value is used along with eql? by the Hash class to determine if two objects reference the same hash key. From this I expect that overwritting the `Object#hash` method changes the Hashing algorithm. But this is only the case for some objets. Consider the following code: ```Ruby class Object def hash i_dont_exist end end class Ana end hash = {} hash[3] = true hash[3] = false puts 'Integers are not using the new hash method' hash[Ana.new] = true puts 'this will not be executed because the class Ana uses the new hash method' ``` The output of executing this code is: ``` Integers are not using the new hash method Traceback (most recent call last): 1: from a.rb:13:in `
' a.rb:3:in `hash': undefined local variable or method `i_dont_exist' for # (NameError) ``` This proves that Integer hash method is not used to determine if two objects reference the same hash key, as said in the documentation. Check the [`any_hash` method](https://github.com/ruby/ruby/blob/master/hash.c#L188) for the other classes affected. I think that either the behavior should be modified (aiming for a consistency along different classes and following the documentation) or the documentation updated. -- https://bugs.ruby-lang.org/ Unsubscribe: