From: Zakaria Date: 2004-06-19T01:16:16+09:00 Subject: Re: How to make generic #== method? On Fri, Jun 18, 2004 at 04:06:32PM +0900, Gavin Sinclair wrote: > Zakaria wrote: > > 2) It doesn't work if the class has hash attribute because > > {'a' => 1}.hash != {'a' => 1}.hash > Now that is weird. I've a faint recollection of it being mentioned on > ruby-talk before though, so perhaps its not a bug. Could someone explain why? > #hash is used to generate a hash code, which is used to put an object into > a hash (i.e. a Hash object). If two objects have the same #hash value, > then a Hash thinks they're probably the same, but it's not guaranteed, so > it double-checks with #eql?. However, if two objects have *different* > #hash values, then a Hash considers them to be definitely different. > Bottom line, if you (re)define #== in an object, you really really should > (re)define #hash as well, to make sure they are consistent. They only needed if I put the object as the hash key right? > > PS: Is there any place where I could post this snippet, > > so others could use it? > As it happens, I've already written a detailed consideration of Joel's > code for the not-yet-formally-announced project 'addlib', so that people > can reuse it without having to manage the code themselves. I hope I could contribute. > Until then, however, you might take a look at the snippet repository on > RubyForge. URL? I know I'm lazy :) > Joel's code is better, however. What yours adds is the implicit > definition of #contents (in Joel's terminology). So why not: I think Robert Klemme version is better and I believe it's faster. And I also like the name, MemberEquivalence. OK going to sleep now > Gavin Wassallam, -- Zakaria z4k4ri4@bigfoot.com Yahoo!: z4k4ri4 http://zakaria.is-a-geek.org http://pemula.linux.or.id