On 9/9/07, Sam Smoot <ssmoot / gmail.com> wrote:

>
> I might suggest you should *never* overwrite String#hash, though you
> may want to overwrite Object#hash in your own classes. It's a bit
> tricky though since the hash should be unique (or close enough), never
> change, and two objects that share the same hash should be
> Object#equal.

No, just the other way around.  Two object which are equal should have
the same hash value.  But there's no requirement that two objects with
the same hash value be equal.

The way hash and equal interact in the implementation of the Hash
class is that comparing two objects hash values acts as a quick test
to rule out the objects being equal.  If they don't have the same hash
they are assumed NOT to be equal.  If they do then equality is tested
for the final determination.

If you want to think of it from an analogy with criminal law, if an
object is suspected of being equal to another object, the hashes must
be the same for an indictment, and the trial consists of actually
testing for equality.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/