On Sep 9, 2:37 pm, Alex Young <a... / blackkettle.org> wrote:
> Ron Green wrote:
> > Peter Cooper wrote:
> >> On 9/9/07, Ron Green <rongre... / mac.com> wrote:
> >>> Marcel Molina Jr. wrote:
> >>>> It should be noted though that String#hash isn't garaunteed to be
> >>>> unique.
> >>> Then,again I ask, what is it good for?
>
> >> It's still useful as a hash. Marcel wasn't wrong, but *no* fixed size
> >> hash
> >> is "guaranteed" to be unique as that's absolutely impossible, per the
> >> pigeonhole principle
> >> (http://en.wikipedia.org/wiki/Pigeonhole_principle).
> >> String#hash's hash is of a far lower "quality" than that offered by,
> >> say,
> >> SHA-1 or SHA-2.
>
> >> Regards,
> >> Peter Cooper
> >>http://www.rubyinside.com/
>
> > Peter,
> > 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.
>
> --
> 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. ;)