From: Yong Li Date: 2011-12-30T18:42:44+09:00 Subject: Re: What’s the standard way of implementing #hash for value objects in Ruby? Maybe you will be interested in reading "Introduction to Algorithms". There is a whole chapter discussing how to implement hash tables including criteria of good hash functions. On Fri, Dec 30, 2011 at 5:09 PM, Nikolai Weibull wrote: > On Fri, Dec 30, 2011 at 06:25, Josh Cheek wrote: >> On Thu, Dec 29, 2011 at 5:47 PM, Nikolai Weibull wrote: >>> So, again, what’s the standard way of implementing #hash for value objects in Ruby? > >> If there is one, I don't know what it is, which implies there isn't one. > > That’s a very bold statement to make, considering that you don’t seem > to know how #hash should be implemented: > >> I usually just delegate to one of my attributes' hash methods (e.g. for a >> user, I might use its user's name's hash). I'm not sure what advantage >> would be gained by establishing a standard. > > If there was a standard way, then no one would have to ask if there > was one.  It would make implementing #hash, which you should/must do > if you implement #==, trivial, as there’s then only one way to do so. > It would make hashing conflicts less likely, as the correct set of > parameters would be used, whatever the object, and a uniform > distribution could be established across different classes of objects. > > Perhaps Ruby’s internal hashing functions (rb_hash_start, > rb_hash_uint, rb_hash_end) could be exposed in some new object? > > Note that these functions are currently not being used in a uniform > way internally.  For example, Struct starts with the #hash of the > object’s class, while Array starts with #length. >