Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

PIVOTAL LABS
Standup 2/18/11 – Overriding Object#hash to change how Hashes store things

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.

Comments
Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *