Hash

Ron G. wrote the following on 09.09.2007 21:48 :

want to override it. It’s there for Hash. From the documentation on
I think I understand. In other words it’s not something I would use
directly.

You could. Hashes are mainly used to restrict the set of objects you
have to look into to find objects identical to one you have or detect
changes in values (with a small margin for false negatives you must be
able to afford).
The Hash class uses it (I’m guessing storing the objects in a balanced
tree using object hashes as keys for quick access).

I used a hash method (not the Ruby’s default one because I wasn’t sure
it would still use the same algorithm in Ruby 3.0… :slight_smile: ) not so long
ago to code a correlation algorithm across text contents. I’ll spare the
details, but I used hashes to get both item lookup speed and storage
space efficiency.

Lionel

On Sep 9, 2:37 pm, Alex Y. [email protected] wrote:

is “guaranteed” to be unique as that’s absolutely impossible, per the
Peter,

Note the direction of implication: a == b => a.hash == b.hash, not
a.hash == b.hash => a == b.


Alex

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. You might also overwrite String#hash in a singleton
class instance, but never the base declaration. That’s just askin’ for
trouble. :wink:

On 9/9/07, Sam S. [email protected] 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/