# Re: Speaking of eql? (was: Ruby and Java equality usage)

**URL:** <https://www.ruby-forum.com/t/re-speaking-of-eql-was-ruby-and-java-equality-usage/96254>\
**Category:** Ruby\
**Created:** [June 27, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/re-speaking-of-eql-was-ruby-and-java-equality-usage/96254 "2006-06-27T18:54:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MolitorS\_Stephen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@MolitorS\_Stephen\_L](https://www.ruby-forum.com/u/MolitorS_Stephen_L)\
**Post date:** [June 27, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/re-speaking-of-eql-was-ruby-and-java-equality-usage/96254/1 "2006-06-27T18:54:03Z")

</div>

> > > > “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Note something that’s peripherally related to == and eql? is the  
> method “hash”, which subclasses MUST override if they override eql?  
> or == so that the guarantee:
> 
> a.eql?(b) implies a.hash == b.hash
> 
> is maintained.

I would reword that slightly. You should override hash if you eql?.  
But not if you only override ‘==’, as ‘==’ is not involved in hashes or  
the hash contract.

Steve

---

<div class="post-metadata">

**Author:** ![MolitorS\_Stephen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@MolitorS\_Stephen\_L](https://www.ruby-forum.com/u/MolitorS_Stephen_L)\
**Post date:** [June 27, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/re-speaking-of-eql-was-ruby-and-java-equality-usage/96254/2 "2006-06-27T20:19:48Z")

</div>

“Molitor, Stephen L” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I would reword that slightly. You should override hash if you eql?.  
> But not if you only override ‘==’, as ‘==’ is not involved in hashes or  
> the hash contract.

The problem with that is that the default definition of eql? is:

def eql?(other)  
self == other  
end

So if you override ==, then you need to override either eql? (to have  
it mean the same as Object#eql?) or hash.

My very strong recommendation, if you’re only going to override one of  
those, would be to override hash (rather than simply overriding eql?  
to mean the same as equal?), since otherwise Hash objects with your  
objects as key won’t behave properly, and neither will Set s of your  
objects.

---

<div class="post-metadata">

**Author:** ![MolitorS\_Stephen\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@MolitorS\_Stephen\_L](https://www.ruby-forum.com/u/MolitorS_Stephen_L)\
**Post date:** [June 27, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/re-speaking-of-eql-was-ruby-and-java-equality-usage/96254/3 "2006-06-27T20:26:20Z")

</div>

Daniel M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> The problem with that is that the default definition of eql? is:
> 
> def eql?(other)  
> self == other  
> end

Never mind. I was misled by the html ruby manual on my system which  
states:

```
    eql?(other)
           Checks if two objects are equal. This method is used by
           Hash to compare whether two keys are same. When this
           method is redefined, the hash method should be updated.
           The default definition of the method eql? is like blow:

```

def eql?(other)  
self == other  
end

I now wonder what version that manual is from, because the text  
doesn’t match what I see on [http://ruby-doc.org/core/](http://ruby-doc.org/core/)

That said, now that it’s clear eql? doesn’t work like that, we should  
say that redefining == without redefining eql? probably isn’t  
something you want to do.
