From: Lionel Bouton Date: 2007-09-10T23:23:11+09:00 Subject: Re: Hash Ron Green wrote the following on 09.09.2007 21:48 : > Alex Young wrote: > >> Ron Green wrote: >> >>>> pigeonhole principle >>>> >>> If Its not guaranteed to be unique, then it can't be used for identity. >>> Can you give me an example of how i would use string.hash? >>> >> In general, you wouldn't use String#hash, although you might conceivably >> want to override it. It's there for Hash. From the documentation on >> Object#hash: >> >> "Generates a Fixnum hash value for this object. This function must have >> the property that a.eql?(b) implies a.hash == b.hash. The hash value is >> used by class Hash." >> >> Note the direction of implication: a == b => a.hash == b.hash, not >> a.hash == b.hash => a == b. >> > > 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... :-) ) 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