# Why does the eql? method exist?

**URL:** <https://www.ruby-forum.com/t/why-does-the-eql-method-exist/72832>\
**Category:** Ruby\
**Created:** [October 19, 2006, 8:01am UTC](https://www.ruby-forum.com/t/why-does-the-eql-method-exist/72832 "2006-10-19T08:01:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [October 19, 2006, 8:01am UTC](https://www.ruby-forum.com/t/why-does-the-eql-method-exist/72832/1 "2006-10-19T08:01:53Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [October 19, 2006, 11:57am UTC](https://www.ruby-forum.com/t/why-does-the-eql-method-exist/72832/2 "2006-10-19T11:57:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [October 19, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/why-does-the-eql-method-exist/72832/3 "2006-10-19T16:37:54Z")

</div>

On 10/19/06, Just Another Victim of the Ambient M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”.
