Jacob F. wrote:
Is it true then that a.eql?(b) only if a.hash == b.hash?
Yes. But not necessarily the vice versa. If a.eql?(b) is true, then
a.hash == b.hash must also be true. And if a.hash != b.hash, then
a.eql?(b) is necessarily false. However if a.hash == b.hash, then
a.eql?(b) may be true or false. (The above is all assuming proper
implementations of hash and eql?.)
What’s the damage in a.eql?(b) returning true when in different
That will never happen. If a and b have different hashes then they go
in different buckets. And if their hashes are different they are not
equivalent (not eql?).
Why doesn’t the default implementation of Object#eql? just use #==
Good question! Or to put it another way why do we need separate == and
eql? methods? The only rationale I can think of is to completely
separate the hash related methods (eql? and hash) from ==. So I can
override == knowing that I won’t be changing hash behavior at all.
Except that if I’m overriding == I would usually want to change the hash
related behaviors to be consistent with my new concept of equivalence!