From: Samuel Tesla Date: 2003-12-27T14:51:49+09:00 Subject: Re: array operations Ferenc Engard writes: > What does hash comparison mean? I have read the Object#hash and > Object#eql? documentation, but they do not mention arrays. Moreover, > there comes the question: what the default Object#hash do? It has a > generic algorithm which computes something based on the object's > attributes? Hash comparison means that it compares the value of Object#hash for each object. This is where a quick look at the Ruby source is very enlightening. Having had to do some battle with these methods recently myself, I have the code fresh in my mind. Object#hash just returns the object ID by default. This is done in C code, so if you override Object#object_id, it will not return what you expect. > Anyway, I have to define both == and eql? in my classes to work > properly? What is the semantical difference of this two method? And then > I have to redefine hash also to ensure that a.eql?(b) implies > a.hash==b.hash? You need to define #eql? and #hash for hash comparisons to work properly, and you need to define #== for the == operator to work. You need to define #=== if you plan on using these in case statements (although it defaults to using #==), and #equal? does not need to be redefined. Hope this helps! -- Samuel