From: Rein Henrichs on 5 Jun 2010 15:06 Correction: > def hash > @email.hash ^ self.class.hash + > end should be: def hash @email.hash ^ self.class.hash + 1 end
From: Robert Dober on 5 Jun 2010 16:09 On Sat, Jun 5, 2010 at 8:55 PM, Rein Henrichs <reinh(a)reinh.com> wrote: <snip> > 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
From: Anderson Leite on 5 Jun 2010 18:30 The first solution sent by Marcin Wolski works for normal ruby classes, but I got something wrong when using with an ActiveRecord class. I am not sure about the problem know. So I tried Robert Klemme's solution, using: in_both = obj_from_db.select do |u1| obj_from_xml.any? do |u2| u1.email == u2.email && u1.age == u2.age end end That's working know. I have to study a little more of your suggestions since this aproach still looks like slowly yet.... To figure the problem, I am using this algorithm at http://www.dobbyme.com That's a birthday reminder. The idea implemented here is: I got your gmail contacts (useim gdata gem) and lokk at dobbyme's database to match if you have some contact already registered. I think now the actual aproach is fine, but with a lot of users I will start to have some troubles...am I right ? Opnions ? -- Posted via http://www.ruby-forum.com/.
From: Rein Henrichs on 5 Jun 2010 18:39 On 2010-06-05 13:09:30 -0700, Robert Dober said: > On Sat, Jun 5, 2010 at 8:55 PM, Rein Henrichs <reinh(a)reinh.com> wrote: > <snip> >> 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
From: Ryan Bigg on 5 Jun 2010 19:05
Robert Dober wrote: > On Sat, Jun 5, 2010 at 8:55 PM, Rein Henrichs <reinh(a)reinh.com> wrote: > <snip> >> 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. I agree with Rein on this. -- Posted via http://www.ruby-forum.com/. |