From: "Aleksi Niemelä" Date: 2001-01-19T22:50:30+09:00 Subject: [ruby-talk:9551] Re: Strange Hash behavior matz says: > >Hash checks equality of keys by `hash' and `eql?'. > >Try to define both. > > Agnot asks: > Ok, thanks. But for me its a violation of "Principle of Least > Surprise". > Can someone give a rationale? It's pretty common for Hash to require these two things. With the method hash you can ask "unique" code for each object, thus achieve O(n) searches. OTOH, you seldom can generate quickly a meaningful hash code which doesn't give duplicates; those are called collisions. I guess it's quite common to first find a right list of key-value pairs through a hash code, and then the right pair inside the list through some equality check. IMHO it's also wise to define eql? instead of blindly use ==, as you might want the hash comparison to be different from normal comparison. - Aleksi