From: Tim Pease Date: 2006-12-20T06:03:46+09:00 Subject: Re: compare two objects without take its ID in consideration On 12/19/06, ara.t.howard@noaa.gov wrote: > > i prefer this pattern for the self documenting properties: > > harp:~ > cat a.rb > class C > %w[ a b c ].each{|a| attr a} > def initialize a, b, c > @a, @b, @c = a, b, c > end > CMP_ATTRS = %w[ a b c ] > def == other > begin > x = CMP_ATTRS.map{|a| send a } > y = CMP_ATTRS.map{|a| other.send a } > x == y > rescue > false > end > end > end > > p( C.new(1,2,3) == C.new(1,2,3) ) > p( C.new(1,2,3) == C.new(2,3,4) ) > > > harp:~ > ruby a.rb > true > false > > > though it too suffers from cyclical structures... sigh. > Infinite recursion only bothers me when I have a finite processor ;) Yeah, you can shoot yourself in the foot no matter how careful you are. I did like Joel's "ContentEquality" pattern elsewhere in this thread. I'm going to have to adopt that one. Blessings, TwP