From: Robert Dober Date: 2010-06-07T02:05:22+09:00 Subject: Re: comparing objects On Sun, Jun 6, 2010 at 1:05 AM, Ryan Bigg wrote: > Robert Dober wrote: >> On Sat, Jun 5, 2010 at 8:55 PM, Rein Henrichs wrote: >> >>> The Array#& operator, like its siblings, uses the #hash and #eql? methods of >>> the objects it compares. These must be the same for objects you want to be >>> considered equivalent. >>> >> I must have made a stupid mistake in my example code, however, this is >> no reason to mess with #eql? and #hash, Be warned. >> In that case I stick with Robert's advice: Do not use Array#&, even >> MPing Array with something like #intersect_by would be preferable. >> > > Wow. Sorry, I am just stunned by this, so no sugar coating for you > today. > > Array#& is a perfectly legitimate method to be using for the purpose of > finding the intersect of two Array objects, which it seems what the OP > wanted to do originally. > Did I say you should not use Array#&??? I do not think so. I said, do not overload Object#hash and Object#eql? for the purpose to use Array#&. And if I was not clear enough I will try again: It will brake any client code that uses your objects as hash keys. Thus you override #hash and #eql? if you want that behavior only. But yes, using Array#& with different object comparison semantics seems a bad idea to me. Please bear in mind that there is no reason to assume that OP has the kind of experience to assume the side effects present (He would not have asked the question in that case). Cheers Robert -- The best way to predict the future is to invent it. -- Alan Kay