Jay PhillipsJay Phillips
Standup 2/18/11 - Overriding Object#hash to change how Hashes store things
edit Posted by Jay Phillips on Friday February 18, 2011 at 07:14PM

Interesting Things

If you want to override how an object is stored in a Hash internally, you can override the Object#hash and Object#eql? methods. Hash always uses Object#hash in its internal hashtable. Try to keep this assertion in mind too:

foo.hash.should == bar.hash if foo.eql?(bar)

Instead of inventing some reliable algorithm to generate your hash code, you can call #hash on a object that can serve as a proxy hash generator. For example, if your object has a #name method, your #hash method could return name.hash.