Ruby and Java equality usage

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?
   int* a; int* b;
   a == b;

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

The equals? method checks identity. As it says here,
http://www.ruby-doc.org/core/classes/Object.html#M001416, the eql?
method can be and usually is overridden to be synonymous with ==. If
you put your objects in a hash you're going to want to override hash and
eql? to have equivalent semantics.

Having three separate equality methods leads to this kind of confusion.
You need two (identity and value equality) but not 3 IHMO.

Steve

···

On Jun 26, 2006 Matthew Smillie wrote:

Oops... it looks like I've touched a nice one :slight_smile: (but somehow I am
happy that my confusion has basis)

./alex

···

--
.w( the_mindstorm )p.
---
(http://themindstorms.blogspot.com)

On 6/26/06, Molitor, Stephen L <Stephen.L.Molitor@erac.com> wrote:

On Jun 26, 2006 Matthew Smillie wrote:

> 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?
> int* a; int* b;
> a == b;

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

The equals? method checks identity. As it says here,
http://www.ruby-doc.org/core/classes/Object.html#M001416, the eql?
method can be and usually is overridden to be synonymous with ==. If
you put your objects in a hash you're going to want to override hash and
eql? to have equivalent semantics.

Having three separate equality methods leads to this kind of confusion.
You need two (identity and value equality) but not 3 IHMO.

Steve