Why does the eql? method exist?

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…

Just Another Victim of the Ambient M. 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

On 10/19/06, Just Another Victim of the Ambient M.
[email protected] 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”.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs