From: Rein Henrichs Date: 2010-06-06T07:40:07+09:00 Subject: Re: comparing objects On 2010-06-05 13:09:30 -0700, Robert Dober said: > 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. > > Cheers > Robert "Be warned"? "Do not use Array#&"? Enough of this baseless FUD. Understand the consequences of what you're doing, certainly, but don't shy away from techniques just because someone else cries wolf. Array#& is meant to be used. Implementing #hash and #eql? in your classes is as normal as implementing #==, #<=> or #each. Ruby's duck typing is one of its most powerful features and should always be preferred over one-off monkey-patching. I'm afraid Robert has it precisely backwards. -- Rein Henrichs http://puppetlabs.com http://reinh.com