Confusion about aliasing eql? and ==

I'm new to ruby and have some confusion about the eql? and == methods. I
understand that eql? compares value and type, while == just compares
values, but the ruby documentation states:

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

It does not seem like this is the implemented behavior of Object, but
rather a suggestion. The object.c source code for ruby states
Subclasses normally continue this tradition by aliasing
<code>eql?</code> to their overridden <code>==</code> method, but there
are exceptions

So my question is - is it typical to just alias :eql? :== in classes
when overriding == and have == check both type and value? If this is the
common case, I would think there is an easier way to do this rather than
adding an alias to every file where you override ==.

Thanks,
Jeff

···

--
Posted via http://www.ruby-forum.com/.

Hi,

Well, eql? is generally just a method you can use to compare more
strictly than the == operator. For example, 1 == 1.0 is true, while
1.eql?(1.0) is false. So the numeric classes use == for "weak
comparisons" (no type checking) and eql? for "strong comparisons".

You may do something similar with your own classes, if you think it
makes sense. If you don't, then the eql? method from Object will be
used, which simply checks for object identity.

I think it's typical to not touch the eql? method at all. I've actually
never used it (not even as an alias for ==).

···

--
Posted via http://www.ruby-forum.com/.

Isn't eql? used for hashing? If you want your objects to be properly
looked up in a Hash, don't you need to use eql?

···

--
Posted via http://www.ruby-forum.com/.

Jeff Storey wrote in post #1066533:

Isn't eql? used for hashing? If you want your objects to be properly
looked up in a Hash, don't you need to use eql?

You *can*, if you want the keys to be compared in a certain way rather
than simply by identity. But you don't have to. The standard definitions
of "eql?" and "hash" should work just fine.

···

--
Posted via http://www.ruby-forum.com/\.