[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Why does the eql? method exist?

Just Another Victim of the Ambient Morality

10/19/2006 5:59:00 AM

Not too long ago, there was a thread about a bug in Ruby where vectors
(which turned out to be derived from matrices) failed to work as hash keys.
The problem turned out to be a typo in the definition of the Matrix class.
The method "eql?" is used by hashes for key comparison but, in the Matrix
class, this method was named "eqn?" by mistake.
This got me thinking about the role of the "eql?" method. Why does it
exist? What is the Ruby rationale behind having two different equality
methods, namely "==" and "eql?"
Thank you...


2 Answers

Esad Hajdarevic

10/19/2006 9:51:00 AM

0

Just Another Victim of the Ambient Morality wrote:

> This got me thinking about the role of the "eql?" method. Why does it
> exist? What is the Ruby rationale behind having two different equality
> methods, namely "==" and "eql?"
> Thank you...

ri Object#eql?

...
The +eql?+ method returns +true+ if _obj_ and _anObject_ have the
same value. Used by +Hash+ to test members for equality. For
objects of class +Object+, +eql?+ is synonymous with +==+.
Subclasses normally continue this tradition, but there are
exceptions. +Numeric+ types, for example, perform type conversion
across +==+, but not across +eql?+, so:
1 == 1.0 #=> true
1.eql? 1.0 #=> false

Greetings,

Esad

Tomasz Wegrzanowski

10/19/2006 2:36:00 PM

0

On 10/19/06, Just Another Victim of the Ambient Morality
<ihatespam@rogers.com> wrote:
> Not too long ago, there was a thread about a bug in Ruby where vectors
> (which turned out to be derived from matrices) failed to work as hash keys.
> The problem turned out to be a typo in the definition of the Matrix class.
> The method "eql?" is used by hashes for key comparison but, in the Matrix
> class, this method was named "eqn?" by mistake.
> This got me thinking about the role of the "eql?" method. Why does it
> exist? What is the Ruby rationale behind having two different equality
> methods, namely "==" and "eql?"

Most languages provide many equality operators,
as depending on context different kind of "equality" is needed.

Obviously we want 1.0 == 1 to be true, right ?
And as obviously, we want a_hash[1.0] and a_hash[1] to be different.
So we have == and eql?, and even equal? for "is-it-the-same-object".

--
Tomasz Wegrzanowski [ http://t-a-w.blo... ]