The Ruby docs read as follows:
The eql? method returns true if obj and other refer to the same hash key.
So in order to use #eql?
to compare two objects (or use objects as Hash keys), the object has to implement #hash
in a meaningful manner.
How come the following happens?
class EqlTest
def hash
123
end
end
a = EqlTest.new
b = EqlTest.new
a.hash == b.hash # => true
a.eql? b # => false
I could of course implement EqlTest#eql?
but shouldn't the implementation inherited from Object
be something along the lines of hash == other.hash
already?
Thanks for your hints!
CodePudding user response:
This seems to be actually the other way around. eql?
is expected to return true
for objects returning the same hash
value, but it is not defined to compare these values. You are simply expected to override both.
The eql? method returns true if obj and other refer to the same hash key. This is used by Hash to test members for equality. For any pair of objects where eql? returns true, the hash value of both objects must be equal. So any subclass that overrides eql? should also override hash appropriately.