# Ruby and Java equality usage

**URL:** <https://rubytalk.org/t/ruby-and-java-equality-usage/28476>\
**Category:** ruby-talk\
**Created:** [26 June 2006 11:38 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476 "2006-06-26T11:38:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandru\_Popescu](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Alexandru\_Popescu](https://rubytalk.org/u/Alexandru_Popescu)\
**Post date:** [26 June 2006 11:38 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/1 "2006-06-26T11:38:32Z")

</div>

Hi!

I have posted a quick (reminder like) entry about how equality is  
handled by Ruby and Java:

> **[A parallel of equality in 2 worlds (Ruby and Java)](http://themindstorms.blogspot.com/2006/06/parallel-of-equality-in-2-worlds-ruby.html)**
>
> Here is a short presentation of equality methods in Ruby and Java : Ruby Java Object#equal? == Object#eql? Object.equals Object#== O...

However, I feel I have missed to explain correctly why eql? and ==  
are both needed (and I've been reading through Ruby books and searched  
the ruby mailing list). Maybe somebody can clarify it and give a  
better example. I will be happy to update the entry to reflect the  
"new" knowledge.

./alex

> **···**
>
> --  
> .w( the\_mindstorm )p.  
> ---  
> ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [26 June 2006 12:01 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/2 "2006-06-26T12:01:55Z")

</div>

> Hi!
> 
> I have posted a quick (reminder like) entry about how equality is  
> handled by Ruby and Java:
> 
> [mindstorms: A parallel of equality in 2 worlds (Ruby and Java)](http://themindstorms.blogspot.com/2006/06/parallel-of-equality-in-2-worlds-ruby.html)
> 
> However, I feel I have missed to explain correctly why eql? and ==  
> are both needed (and I've been reading through Ruby books and searched  
> the ruby mailing list). Maybe somebody can clarify it and give a  
> better example. I will be happy to update the entry to reflect the  
> "new" knowledge.

I am surprised you did not find this reference, possibly I missed a point in  
your mail  
[http://www.ruby-doc.org/core/classes/Object.html#M001416](http://www.ruby-doc.org/core/classes/Object.html#M001416)  
if I interpret it correctly

== is supposed to be redefined in classes in order to reflect identity on an  
"application" level.  
equal? shall not be redefined in order to be available for object identity,  
something like  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.equal?(b) iff a.object\_id == b.object\_id  
eql? is used for Hash entry comparison and is not redefined most of the time

Just citing ruby doc for those who do not want to follow the link 😉

Cheers  
Robert

./alex

> **···**
>
> On 6/26/06, Alexandru Popescu \<the.mindstorm.mailinglist@gmail.com\> wrote:
> 
> > --  
> > .w( the\_mindstorm )p.  
> > ---  
> > ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [26 June 2006 21:28 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/3 "2006-06-26T21:28:55Z")

</div>

#eql? is used to resolve key collisions in hashes.

If two objects have the same #hash and are #eql? then they refer to the same hash key. If two objects have the same #hash and are not #eql? they refer to different hash keys.

> **···**
>
> On Jun 26, 2006, at 4:38 AM, Alexandru Popescu wrote:
> 
> > I have posted a quick (reminder like) entry about how equality is  
> > handled by Ruby and Java:
> > 
> > [mindstorms: A parallel of equality in 2 worlds (Ruby and Java)](http://themindstorms.blogspot.com/2006/06/parallel-of-equality-in-2-worlds-ruby.html)
> > 
> > However, I feel I have missed to explain correctly why eql? and ==  
> > are both needed.
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Alexandru\_Popescu](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Alexandru\_Popescu](https://rubytalk.org/u/Alexandru_Popescu)\
**Post date:** [26 June 2006 12:09 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/4 "2006-06-26T12:09:41Z")

</div>

Thanks Robert. I have used that information and I am even linking to  
it. The problem I was facing was to explain why == and eql? are both  
needed, and when their implementation may be different.

./alex

> **···**
>
> --  
> .w( the\_mindstorm )p.  
> ---  
> ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))
> 
> On 6/26/06, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > On 6/26/06, Alexandru Popescu \<the.mindstorm.mailinglist@gmail.com\> wrote:  
> > \>  
> > \> Hi!  
> > \>  
> > \> I have posted a quick (reminder like) entry about how equality is  
> > \> handled by Ruby and Java:  
> > \>  
> > \> [mindstorms: A parallel of equality in 2 worlds (Ruby and Java)](http://themindstorms.blogspot.com/2006/06/parallel-of-equality-in-2-worlds-ruby.html)  
> > \>  
> > \> However, I feel I have missed to explain correctly why eql? and ==  
> > \> are both needed (and I've been reading through Ruby books and searched  
> > \> the ruby mailing list). Maybe somebody can clarify it and give a  
> > \> better example. I will be happy to update the entry to reflect the  
> > \> "new" knowledge.
> > 
> > I am surprised you did not find this reference, possibly I missed a point in  
> > your mail  
> > [class Object - RDoc Documentation](http://www.ruby-doc.org/core/classes/Object.html#M001416)  
> > if I interpret it correctly
> > 
> > == is supposed to be redefined in classes in order to reflect identity on an  
> > "application" level.  
> > equal? shall not be redefined in order to be available for object identity,  
> > something like  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.equal?(b) iff a.object\_id == b.object\_id  
> > eql? is used for Hash entry comparison and is not redefined most of the time
> > 
> > Just citing ruby doc for those who do not want to follow the link 😉
> > 
> > Cheers  
> > Robert
> > 
> > ./alex  
> > \> --  
> > \> .w( the\_mindstorm )p.  
> > \> ---  
> > \> ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))  
> > \>
> > 
> > --  
> > Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> > concerne l'univers, je n'en ai pas acquis la certitude absolue.
> > 
> > - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 June 2006 14:42 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/5 "2006-06-26T14:42:30Z")

</div>

> \> However, I feel I have missed to explain correctly why eql? and ==  
> \> are both needed (and I've been reading through Ruby books and searched  
> \> the ruby mailing list). Maybe somebody can clarify it and give a  
> \> better example. I will be happy to update the entry to reflect the  
> \> "new" knowledge.
> 
> I am surprised you did not find this reference, possibly I missed a point in  
> your mail  
> [class Object - RDoc Documentation](http://www.ruby-doc.org/core/classes/Object.html#M001416)  
> if I interpret it correctly
> 
> == is supposed to be redefined in classes in order to reflect identity on an  
> "application" level.  
> equal? shall not be redefined in order to be available for object identity,  
> something like  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.equal?(b) iff a.object\_id == b.object\_id  
> eql? is used for Hash entry comparison and is not redefined most of the time

I don't fully agree to your point. Basically == and eql? are meant to  
implement object equivalence. So if you implement == you should be  
implementing eql? and hash, too. And usually == and eql? should  
behave the same. The page you mention quotes "For objects of class  
Object, eql? is synonymous with ==. Subclasses normally continue this  
tradition, but there are exceptions.". Numbers make a suble difference  
here:

> > 1 == 1.0

=\> true

> > 1.eql? 1.0

=\> false

Personally I find this situation a bit odd. I'd prefer a single  
equivalence relation per class not two.

Kind regards

robert

> **···**
>
> 2006/6/26, Robert Dober \<robert.dober@gmail.com\>:
> 
> > On 6/26/06, Alexandru Popescu \<the.mindstorm.mailinglist@gmail.com\> wrote:
> 
> --  
> Have a look: [Robert K. | Flickr](http://www.flickr.com/photos/fussel-foto/)

---

<div class="post-metadata">

**Author:** ![Alexandru\_Popescu](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Alexandru\_Popescu](https://rubytalk.org/u/Alexandru_Popescu)\
**Post date:** [27 June 2006 00:06 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/6 "2006-06-27T00:06:54Z")

</div>

... and still wondering how is this answering my question (however  
thanks for the intention).

./alex

> **···**
>
> On 6/27/06, Eric Hodel \<drbrain@segment7.net\> wrote:
> 
> > On Jun 26, 2006, at 4:38 AM, Alexandru Popescu wrote:
> > 
> > \> I have posted a quick (reminder like) entry about how equality is  
> > \> handled by Ruby and Java:  
> > \>  
> > \> [http://themindstorms.blogspot.com/2006/06/parallel-of-equality-in-2-](http://themindstorms.blogspot.com/2006/06/parallel-of-equality-in-2-)  
> > \> worlds-ruby.html  
> > \>  
> > \> However, I feel I have missed to explain correctly why eql? and ==  
> > \> are both needed.
> > 
> > #eql? is used to resolve key collisions in hashes.
> > 
> > If two objects have the same #hash and are #eql? then they refer to  
> > the same hash key. If two objects have the same #hash and are not  
> > #eql? they refer to different hash keys.
> 
> --  
> .w( the\_mindstorm )p.  
> ---  
> ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [26 June 2006 12:23 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/7 "2006-06-26T12:23:34Z")

</div>

Personally I find it quite useful to have "==" which I can adapt to my  
needs, knowing that I can always count on "eql?" for object equality, I  
might do things like this:

class MyString \< String  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ==( otherString )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise SomeException unless String === otherString  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.downcase.eql?( otherString.downcase )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

x=MyString.new("Alfa")  
x == "alfa" ==\> true  
x.eql?("alfa") ==\> false

Now I think it is perfectly sane to say, no I think this is confusing I will  
keep my "==" and my "eql?" identical.  
However be prepared that others will not 😉

Cheers  
Robert

> **···**
>
> On 6/26/06, Alexandru Popescu \<the.mindstorm.mailinglist@gmail.com\> wrote:
> 
> > Thanks Robert. I have used that information and I am even linking to  
> > it. The problem I was facing was to explain why == and eql? are both  
> > needed, and when their implementation may be different.
> > 
> > Ok I knew I missed something.

---

<div class="post-metadata">

**Author:** ![Matthew\_Smillie](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Matthew\_Smillie](https://rubytalk.org/u/Matthew_Smillie)\
**Post date:** [26 June 2006 13:39 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/8 "2006-06-26T13:39:10Z")

</div>

I'll give you an actual use-case from my current work in natural language processing. I have Word objects which have various properties, not all of which are important to 'equality' for Words (and which may vary for two equal words). As an example, a verb might have two different subjects in two different contexts - from a Word-oriented perspective, they're identical, from other perspectives they may not be.

I define '==' to reflect what equality should be for words, something like this:

class Word  
&nbsp;&nbsp;&nbsp;attr :stem, :text, :category, :pos, :etc  
&nbsp;&nbsp;&nbsp;def ==(word)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# a 'stem' is the root form of a word,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# e.g., 'looking'.stem =\> 'look'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.stem == word.stem  
&nbsp;&nbsp;&nbsp;end  
end

On the other hand, when I'm comparing words, I want to make sure I'm not generating spurious matches by comparing a particular Word to itself, so I check for that with the default implementation of eql? The only way to avoid using eql? would be to invent something functionally equivalent - maybe a variable to hold the precise position of the word in the text, and then a predicate method to compare positions. But eql? is there, so that's what I use.

Coming from C, I tend to think of Ruby variables as pointers to values, so I tend to think of the difference between eql? and == as akin to the following:

Ruby eql? - i.e., are they at the same spot in memory?  
&nbsp;&nbsp;&nbsp;int\* a; int\* b;  
&nbsp;&nbsp;&nbsp;a == b;

Ruby == - i.e., do they contain the same value?  
&nbsp;&nbsp;&nbsp;int\* a; int\* b;  
&nbsp;&nbsp;&nbsp;\*a == \*b;

So, in a general case, I'd say that you'd implement == and eql? differently when you can have multiple objects where identity is defined on a subset of their attributes (including their object id). For post authors on a mailing list, to make up a simple example:

class Author  
&nbsp;&nbsp;&nbsp;attr :email, :name  
&nbsp;&nbsp;&nbsp;def ==(other)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# one email may have many names:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Matthew Smillie, matt, Matthew B Smillie, ...)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.email == other.email  
&nbsp;&nbsp;&nbsp;end  
end

matthew smillie

[1] - a stem is the root form of a word, e.g., 'looking' -\> 'look', 'bridges' -\> 'bridge' and so forth. Google on 'Porter Stemmer' for a vast amount of background and a popular implementation.

> **···**
>
> On Jun 26, 2006, at 13:09, Alexandru Popescu wrote:
> 
> > Thanks Robert. I have used that information and I am even linking to  
> > it. The problem I was facing was to explain why == and eql? are both  
> > needed, and when their implementation may be different.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [26 June 2006 16:29 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/9 "2006-06-26T16:29:21Z")

</div>

> \> \> However, I feel I have missed to explain correctly why eql? and ==  
> \> \> are both needed (and I've been reading through Ruby books and searched  
> \> \> the ruby mailing list). Maybe somebody can clarify it and give a  
> \> \> better example. I will be happy to update the entry to reflect the  
> \> \> "new" knowledge.  
> \>  
> \> I am surprised you did not find this reference, possibly I missed a  
> point in  
> \> your mail  
> \> [class Object - RDoc Documentation](http://www.ruby-doc.org/core/classes/Object.html#M001416)  
> \> if I interpret it correctly  
> \>  
> \> == is supposed to be redefined in classes in order to reflect identity  
> on an  
> \> "application" level.  
> \> equal? shall not be redefined in order to be available for object  
> identity,  
> \> something like  
> \> a.equal?(b) iff a.object\_id == b.object\_id  
> \> eql? is used for Hash entry comparison and is not redefined most of the  
> time
> 
> I don't fully agree to your point. Basically == and eql? are meant to  
> implement object equivalence. So if you implement == you should be  
> implementing eql? and hash, too. And usually == and eql? should  
> behave the same. The page you mention quotes "For objects of class  
> Object, eql? is synonymous with ==. Subclasses normally continue this  
> tradition, but there are exceptions.". Numbers make a suble difference  
> here:
> 
> \>\> 1 == 1.0  
> =\> true  
> \>\> 1.eql? 1.0  
> =\> false
> 
> Personally I find this situation a bit odd. I'd prefer a single  
> equivalence relation per class not two.
> 
> Kind regards
> 
> robert
> 
> --  
> Have a look: [http://www.flickr.com/photos/fussel-foto/](http://www.flickr.com/photos/fussel-foto/)

> **···**
>
> On 6/26/06, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > 2006/6/26, Robert Dober \<robert.dober@gmail.com\>:  
> > \> On 6/26/06, Alexandru Popescu \<the.mindstorm.mailinglist@gmail.com\> \> wrote:
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [26 June 2006 16:38 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/10 "2006-06-26T16:38:56Z")

</div>

> \<SNIP\>
> 
> I don't fully agree to your point. Basically == and eql? are meant to  
> implement object equivalence. So if you implement == you should be  
> implementing eql? and hash, too. And usually == and eql? should  
> behave the same. The page you mention quotes "For objects of class  
> Object, eql? is synonymous with ==. Subclasses normally continue this  
> tradition, but there are exceptions.". Numbers make a suble difference  
> here:

Are you sure you read the page correctly?  
I copy paste:

\<begin doc\>  
obj == other =\> true or false  
obj.equal?(other) =\> true or false  
obj.eql?(other) =\> true or false  
\<[http://www.ruby-doc.org/core/classes/Object.src/M001416.html&gt](http://www.ruby-doc.org/core/classes/Object.src/M001416.html&gt);

Equality—At the Object \<[http://www.ruby-doc.org/core/classes/Object.html&gt;level](http://www.ruby-doc.org/core/classes/Object.html&gt;level),  
== returns true only if \*obj\* and \*other\* are the same object. Typically,  
this method is overridden in descendent classes to provide class-specific  
meaning.

Unlike ==, the equal? method should never be overridden by subclasses: it is  
used to

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

determine object identity (that is, a.equal?(b) iff a is the same object as  
b).

The eql? method returns true if \*obj\* and \*anObject\* have the same value.  
Used by Hash \<[http://www.ruby-doc.org/core/classes/Hash.html&gt](http://www.ruby-doc.org/core/classes/Hash.html&gt); to test  
members for equality. For objects of class  
Object\<[http://www.ruby-doc.org/core/classes/Object.html&gt;](http://www.ruby-doc.org/core/classes/Object.html&gt;),  
eql? is synonymous with ==. Subclasses normally continue this tradition, but  
there are exceptions.  
Numeric\<[http://www.ruby-doc.org/core/classes/Numeric.html&gt;types](http://www.ruby-doc.org/core/classes/Numeric.html&gt;types), for

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

example, perform type conversion across ==, but not across eql?, so:

&nbsp;&nbsp;&nbsp;1 == 1.0 #=\> true  
&nbsp;&nbsp;&nbsp;1.eql? 1.0 #=\> false

\<end doc\>  
Now unless this is wrong, one can conclude that  
\* #equal? shall not be redefined,  
\* #eql? should not be redefined, unless 1's knowing what one's doing (not 4  
me ;=)  
but  
\* #== can be redefined freely.

> > 1 == 1.0
> 
> =\> true  
> \>\> 1.eql? 1.0  
> =\> false

Personally I find this situation a bit odd. I'd prefer a single

> equivalence relation per class not two.

Although I do not feel the same that seems a normal concern, as I pointed  
out to the OP.  
Honestly I think it depends on the programming culture we are coming from  
and when  
in our mind these are synonyms we are likely to get bitten by that kind of  
code.  
I can only offer you my sympathy not my agreement.

But if we discuss this long enough it will become completely engraved in our  
minds in we will be aware of it even if we do not like it 😉

Cheers  
Robert

Kind regards

> **···**
>
> On 6/26/06, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > 2006/6/26, Robert Dober \<robert.dober@gmail.com\>:
> > 
> > robert
> > 
> > --  
> > Have a look: [http://www.flickr.com/photos/fussel-foto/](http://www.flickr.com/photos/fussel-foto/)

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [27 June 2006 07:34 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/11 "2006-06-27T07:34:45Z")

</div>

> > \> I have posted a quick (reminder like) entry about how equality is  
> > \> handled by Ruby and Java:  
> > \>  
> > \> [http://themindstorms.blogspot.com/2006/06/parallel-of-equality-in-2-](http://themindstorms.blogspot.com/2006/06/parallel-of-equality-in-2-)  
> > \> worlds-ruby.html  
> > \>  
> > \> However, I feel I have missed to explain correctly why eql? and ==  
> > \> are both needed.
> > 
> > #eql? is used to resolve key collisions in hashes.

^^^ This line is the important one. I feel it describes adequately what #eql? is needed for.

> > If two objects have the same #hash and are #eql? then they refer to  
> > the same hash key. If two objects have the same #hash and are not  
> > #eql? they refer to different hash keys.
> 
> ... and still wondering how is this answering my question (however  
> thanks for the intention).

#eql? is needed because #== doesn't do what #eql? does.

Or where you looking for a different answer?

> **···**
>
> On Jun 26, 2006, at 5:06 PM, Alexandru Popescu wrote:
> 
> > On 6/27/06, Eric Hodel \<drbrain@segment7.net\> wrote:
> > 
> > > On Jun 26, 2006, at 4:38 AM, Alexandru Popescu wrote:
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [27 June 2006 08:30 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/12 "2006-06-27T08:30:28Z")

</div>

> ... and still wondering how is this answering my question (however  
> thanks for the intention).

Sorry for being blunt, you have started a thread, but that does not mean  
that the thread will stay focused on your initial question, many long  
threads do not.  
At the beginning of course 90% of the posters will try to do so, and so did  
I but than things  
will take a natural drift depending on the point of views of the posters.

I am sorry if you feel you did not get enough out of your question but these  
things happen :(.  
On the other hand you have created a thread that has generated a  
philosophical and civilized discussion, that is already a marvellous thing  
:).

Now I would love to answer your question but maybe it would be easier if you  
take a stronger position, like e.g. I think overriding == is harmfull  
because or something like this.

I strongly feel that there is no \*answer\*, points of views shift slowly,  
they do not jump, they depend so much on what you read or do.  
A majority of people - of course I am not in, as usual ☹ - agree with your  
pov, that also is something you should cheer about 😉

Just my pov, hopefully cheered you up a bit.  
Robert

./alex

> **···**
>
> > --  
> > .w( the\_mindstorm )p.  
> > ---  
> > ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))

---

<div class="post-metadata">

**Author:** ![Alexandru\_Popescu](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Alexandru\_Popescu](https://rubytalk.org/u/Alexandru_Popescu)\
**Post date:** [26 June 2006 12:41 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/13 "2006-06-26T12:41:49Z")

</div>

Thanks for confirming my point :-). I couldn't find a  
good/workable/meaningfull example for having different implementation  
for == and eql?. I am still looking for.

./alex

> **···**
>
> On 6/26/06, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > On 6/26/06, Alexandru Popescu \<the.mindstorm.mailinglist@gmail.com\> wrote:  
> > \>  
> > \> Thanks Robert. I have used that information and I am even linking to  
> > \> it. The problem I was facing was to explain why == and eql? are both  
> > \> needed, and when their implementation may be different.  
> > \>  
> > \> Ok I knew I missed something.  
> > Personally I find it quite useful to have "==" which I can adapt to my  
> > needs, knowing that I can always count on "eql?" for object equality, I  
> > might do things like this:
> > 
> > class MyString \< String  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def ==( otherString )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise SomeException unless String === otherString  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.downcase.eql?( otherString.downcase )  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > x=MyString.new("Alfa")  
> > x == "alfa" ==\> true  
> > x.eql?("alfa") ==\> false
> > 
> > Now I think it is perfectly sane to say, no I think this is confusing I will  
> > keep my "==" and my "eql?" identical.  
> > However be prepared that others will not 😉
> > 
> > Cheers  
> > Robert
> 
> --  
> .w( the\_mindstorm )p.  
> ---  
> ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [26 June 2006 17:20 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/14 "2006-06-26T17:20:53Z")

</div>

> Are you sure you read the page correctly?

Pretty sure.

> I copy paste:

\<snip/\>

> Now unless this is wrong, one can conclude that  
> \* #equal? shall not be redefined,

Agreed.

> \* #eql? should not be redefined, unless 1's knowing what one's doing (not 4  
> me ;=)

Disagreed. If you redefine == then you should usually redefine eql?  
as well and with similar semantics:

"[Method ==] Typically, this method is overridden in descendent  
classes to provide class-specific meaning."

"For objects of class Object, eql? is synonymous with ==. Subclasses  
normally continue this tradition, but there are exceptions."

So for subclasses == and eql? are usually synonymous even if == is redefined.

> but  
> \* #== can be redefined freely.

"Freely" is a bit too much freedom here for my taste. Of course we  
can implement all these methods in any way we like to, but for a  
typical class == and eql? should reflect equivalence of objects  
(you're probably mean the same, I'd just like to make the point in  
order to help with the engraving :-)). And this in turn usually  
depends on instance variables. So the behavior of Struct.new (which  
defines a new class with a set of attributes) is pretty much the way  
to go \*usually\*. There are other cases where only some attributes  
should be used or even the default Object behavior retained but the  
rule of thumb should be, if you implement ==, then also implement eql?  
(for example by making it an alias) and hash and have all compare  
based on member variables / attributes / observable state.

> Personally I find this situation a bit odd. I'd prefer a single  
> \> equivalence relation per class not two.
> 
> Although I do not feel the same that seems a normal concern, as I pointed  
> out to the OP.  
> Honestly I think it depends on the programming culture we are coming from  
> and when  
> in our mind these are synonyms we are likely to get bitten by that kind of  
> code.

At the moment I'm not so sure whether culture is the background. I  
tend to think that there's a more fundamental issue: having a single  
equivalence relation defined on instances of a class seems to make  
things easier in several areas and I believe that it rather reduces  
programming errors.

> I can only offer you my sympathy not my agreement.

Although sympathy is better than nothing I'd rather convince you. 🙂

> But if we discuss this long enough it will become completely engraved in our  
> minds in we will be aware of it even if we do not like it 😉

I'm the last one not to help engrave something like this in  
everybody's minds so I'll happily continue this thread. 🙂

Kind regards

robert

> **···**
>
> 2006/6/26, Robert Dober \<robert.dober@gmail.com\>:
> 
> --  
> Have a look: [Robert K. | Flickr](http://www.flickr.com/photos/fussel-foto/)

---

<div class="post-metadata">

**Author:** ![Alexandru\_Popescu](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Alexandru\_Popescu](https://rubytalk.org/u/Alexandru_Popescu)\
**Post date:** [27 June 2006 08:23 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/15 "2006-06-27T08:23:04Z")

</div>

Quoting myself:

The problem I was facing was to explain why == and eql? are both  
needed, and when their implementation may be different.

The problem I see is not that #eql? is needed because #== doesn't do  
what #eql? does (because if you look logically at this sentence it  
doesn't make much sense: there is an infinitiy of methods xxx that  
should be needed because #== doesn't do what #xxx does 🙂 ).

I see no good reasons, and I haven't found any good reference for  
cases where these methods are differently implemented. At this moment  
my interpretation is:

#eql? is just syntactic sugar of #==, needed for objects used in  
hashes (because hashes use #eql? and not #==).

./alex

> **···**
>
> On 6/27/06, Eric Hodel \<drbrain@segment7.net\> wrote:
> 
> > On Jun 26, 2006, at 5:06 PM, Alexandru Popescu wrote:
> > 
> > \> On 6/27/06, Eric Hodel \<drbrain@segment7.net\> wrote:  
> > \>\> On Jun 26, 2006, at 4:38 AM, Alexandru Popescu wrote:  
> > \>\>  
> > \>\> \> I have posted a quick (reminder like) entry about how equality is  
> > \>\> \> handled by Ruby and Java:  
> > \>\> \>  
> > \>\> \> [http://themindstorms.blogspot.com/2006/06/parallel-of-equality-](http://themindstorms.blogspot.com/2006/06/parallel-of-equality-)  
> > \>\> in-2-  
> > \>\> \> worlds-ruby.html  
> > \>\> \>  
> > \>\> \> However, I feel I have missed to explain correctly why eql? and ==  
> > \>\> \> are both needed.  
> > \>\>  
> > \>\> #eql? is used to resolve key collisions in hashes.
> > 
> > ^^^ This line is the important one. I feel it describes adequately  
> > what #eql? is needed for.
> > 
> > \>\> If two objects have the same #hash and are #eql? then they refer to  
> > \>\> the same hash key. If two objects have the same #hash and are not  
> > \>\> #eql? they refer to different hash keys.  
> > \>  
> > \> ... and still wondering how is this answering my question (however  
> > \> thanks for the intention).
> > 
> > #eql? is needed because #== doesn't do what #eql? does.
> > 
> > Or where you looking for a different answer?
> 
> --  
> .w( the\_mindstorm )p.  
> ---  
> ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))

---

<div class="post-metadata">

**Author:** ![Alexandru\_Popescu](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Alexandru\_Popescu](https://rubytalk.org/u/Alexandru_Popescu)\
**Post date:** [27 June 2006 08:51 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/16 "2006-06-27T08:51:22Z")

</div>

Rober, thanks and thanks. I think you are right. I am a little sad  
because I have hoped to find a good answer to this, so that others  
will have a good reference for this subject and will not have to pass  
through phylosophical times as I had when reading for the first time  
about equality in Ruby.

Unfortunately, even if I play with Ruby for almost 2 years, I don't  
consider that I have enough knowledge to come up with authoritative  
posts, so this is the reason for my mildy posts. If this would have  
been on a Java subject, things would have been completely different  
;-), but here I just try to keep myself "low profile" and extract as  
information as possible.

Still, I have formulated a conclusion in the previous post, and that  
will be the one that will go to the entry for update:

#eql? is just syntactic sugar of #==, needed for objects used as keys  
in hashes (because hash implementation doesn't like to use #==, but  
only #eql?). If your class needs to override #==, than just delegate  
#eql? implementation to #==.

cheers and once again thanks,

./alex

> **···**
>
> --  
> .w( the\_mindstorm )p.  
> ---  
> ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))
> 
> On 6/27/06, Robert Dober \<robert.dober@gmail.com\> wrote:
> 
> > \> ... and still wondering how is this answering my question (however  
> > \> thanks for the intention).
> > 
> > Sorry for being blunt, you have started a thread, but that does not mean  
> > that the thread will stay focused on your initial question, many long  
> > threads do not.  
> > At the beginning of course 90% of the posters will try to do so, and so did  
> > I but than things  
> > will take a natural drift depending on the point of views of the posters.
> > 
> > I am sorry if you feel you did not get enough out of your question but these  
> > things happen :(.  
> > On the other hand you have created a thread that has generated a  
> > philosophical and civilized discussion, that is already a marvellous thing  
> > :).
> > 
> > Now I would love to answer your question but maybe it would be easier if you  
> > take a stronger position, like e.g. I think overriding == is harmfull  
> > because or something like this.
> > 
> > I strongly feel that there is no \*answer\*, points of views shift slowly,  
> > they do not jump, they depend so much on what you read or do.  
> > A majority of people - of course I am not in, as usual ☹ - agree with your  
> > pov, that also is something you should cheer about 😉
> > 
> > Just my pov, hopefully cheered you up a bit.  
> > Robert
> > 
> > ./alex  
> > \> --  
> > \> .w( the\_mindstorm )p.  
> > \> ---  
> > \> ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))  
> > \>

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [26 June 2006 13:04 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/17 "2006-06-26T13:04:30Z")

</div>

> \>
> 
> Thanks for confirming my point :-). I couldn't find a

Hey you will get me into trouble, I did \*not\* agree with your point, I  
\*accept\* and \*understand\* it, my oppinion is not the only reasonable 😉  
seriously I think it is a feature, not the most important and maybe not the  
least dangerous but a feature nontheless.  
Cheers  
Robert

good/workable/meaningfull example for having different implementation

> **···**
>
> > for == and eql?. I am still looking for.
> > 
> > ./alex  
> > --  
> > .w( the\_mindstorm )p.  
> > ---  
> > ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Elliot\_Temple](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Elliot\_Temple](https://rubytalk.org/u/Elliot_Temple)\
**Post date:** [26 June 2006 17:37 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/18 "2006-06-26T17:37:30Z")

</div>

Perhaps the issue is not so much having more than one, but having unhelpful names for them. We don't get confused by the variety of each-like methods available. And no one forgets the difference between times, upto, and downto, despite their similarities. If we had ==, same\_place\_in\_memory, same\_in\_hash, and same\_object\_id (or whatever they are) people wouldn't get confused even with lots of them.

-- Elliot Temple

> **[Curiosity Blog – Elliot Temple](https://www.curi.us/blog/)**
>
> A philosophy blog

> **···**
>
> On Jun 26, 2006, at 10:20 AM, Robert Klemme wrote:
> 
> > 2006/6/26, Robert Dober \<robert.dober@gmail.com\>:
> > 
> > > Personally I find this situation a bit odd. I'd prefer a single  
> > > \> equivalence relation per class not two.
> > > 
> > > Although I do not feel the same that seems a normal concern, as I pointed  
> > > out to the OP.  
> > > Honestly I think it depends on the programming culture we are coming from  
> > > and when  
> > > in our mind these are synonyms we are likely to get bitten by that kind of  
> > > code.
> > 
> > At the moment I'm not so sure whether culture is the background. I  
> > tend to think that there's a more fundamental issue: having a single  
> > equivalence relation defined on instances of a class seems to make  
> > things easier in several areas and I believe that it rather reduces  
> > programming errors.

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [27 June 2006 09:04 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/19 "2006-06-27T09:04:45Z")

</div>

> Rober, thanks and thanks. I think you are right. I am a little sad  
> because I have hoped to find a good answer to this, so that others  
> will have a good reference for this subject and will not have to pass  
> through phylosophical times as I had when reading for the first time  
> about equality in Ruby.
> 
> Unfortunately, even if I play with Ruby for almost 2 years, I don't  
> consider that I have enough knowledge to come up with authoritative  
> posts, so this is the reason for my mildy posts. If this would have  
> been on a Java subject, things would have been completely different  
> ;-), but here I just try to keep myself "low profile" and extract as  
> information as possible.

You are doing a great job.

Still, I have formulated a conclusion in the previous post, and that

> will be the one that will go to the entry for update:
> 
> #eql? is just syntactic sugar of #==, needed for objects used as keys  
> in hashes (because hash implementation doesn't like to use #==, but  
> only #eql?). If your class needs to override #==, than just delegate  
> #eql? implementation to #==.

Please be aware that this is in contradiction to the official documentation.  
Maybe the doc is wrong but nobody has claimed that so far.  
It will also break Hash lookup unless the reimplementation of #== is very  
careful!  
( I will try to demonstrate this as soon as I have a little time )

Hopefully everybody is backing me up on this one even if they think that is  
how it \*should\* be. Right now it is \*not\* how it is.

Cheers  
Robert

cheers and once again thanks,

> **···**
>
> On 6/27/06, Alexandru Popescu \<the.mindstorm.mailinglist@gmail.com\> wrote:
> 
> > ./alex  
> > --  
> > .w( the\_mindstorm )p.  
> > ---  
> > ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))
> > 
> > On 6/27/06, Robert Dober \<robert.dober@gmail.com\> wrote:  
> > \> \> ... and still wondering how is this answering my question (however  
> > \> \> thanks for the intention).  
> > \>  
> > \> Sorry for being blunt, you have started a thread, but that does not  
> > mean  
> > \> that the thread will stay focused on your initial question, many long  
> > \> threads do not.  
> > \> At the beginning of course 90% of the posters will try to do so, and so  
> > did  
> > \> I but than things  
> > \> will take a natural drift depending on the point of views of the  
> > posters.  
> > \>  
> > \> I am sorry if you feel you did not get enough out of your question but  
> > these  
> > \> things happen :(.  
> > \> On the other hand you have created a thread that has generated a  
> > \> philosophical and civilized discussion, that is already a marvellous  
> > thing  
> > \> :).  
> > \>  
> > \> Now I would love to answer your question but maybe it would be easier if  
> > you  
> > \> take a stronger position, like e.g. I think overriding == is harmfull  
> > \> because or something like this.  
> > \>  
> > \> I strongly feel that there is no \*answer\*, points of views shift slowly,  
> > \> they do not jump, they depend so much on what you read or do.  
> > \> A majority of people - of course I am not in, as usual ☹ - agree with  
> > your  
> > \> pov, that also is something you should cheer about 😉  
> > \>  
> > \> Just my pov, hopefully cheered you up a bit.  
> > \> Robert  
> > \>  
> > \> ./alex  
> > \> \> --  
> > \> \> .w( the\_mindstorm )p.  
> > \> \> ---  
> > \> \> ([http://themindstorms.blogspot.com](http://themindstorms.blogspot.com))  
> > \> \>  
> > \>
> 
> --  
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui  
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
> 
> - Albert Einstein

---

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [27 June 2006 10:01 UTC](https://rubytalk.org/t/ruby-and-java-equality-usage/28476/20 "2006-06-27T10:01:39Z")

</div>

Alexandru Popescu wrote:

> Rober, thanks and thanks. I think you are right. I am a little sad  
> because I have hoped to find a good answer to this, so that others  
> will have a good reference for this subject and will not have to pass  
> through phylosophical times as I had when reading for the first time  
> about equality in Ruby.
> 
> Unfortunately, even if I play with Ruby for almost 2 years, I don't  
> consider that I have enough knowledge to come up with authoritative  
> posts, so this is the reason for my mildy posts. If this would have  
> been on a Java subject, things would have been completely different  
> ;-), but here I just try to keep myself "low profile" and extract as  
> information as possible.
> 
> Still, I have formulated a conclusion in the previous post, and that  
> will be the one that will go to the entry for update:
> 
> #eql? is just syntactic sugar of #==, needed for objects used as keys  
> in hashes (because hash implementation doesn't like to use #==, but  
> only #eql?). If your class needs to override #==, than just delegate  
> #eql? implementation to #==.

Just to weigh in here on how I think of it, without this necessarily being representative of any section of reality: #eql? is used for value-and-type equality, where #== is used for value-equivalence. Thus 2.0 == 2 #=\> true, 2.eql? 2.0 #=\> false. For most types, they'll be identical, because 98 classes out of 100 simply don't have a value-equivalence relationship.

Of course, I could be very, very wrong about this, partially because the only example I can think of off the top of my head where this is the case is Fixnum == Float, and partially because it implies a certain flexibility to the strongly-typed approach which seems quite uncharacteristic.

> **···**
>
> --  
> Alex

[Next page](https://rubytalk.org/t/ruby-and-java-equality-usage/28476.md?page=2)
