From: ara.t.howard@... Date: 2006-10-11T00:44:36+09:00 Subject: Re: Removing Duplicate Objects from Object List On Wed, 11 Oct 2006, Mike wrote: > uniq is failing because, even though the attributes of each instance of > the rule is 'eq' to the other, the compared instances are different. > > class Foo > def initialize(a,b) > @a = a > @b = b > end > end > > x = [Foo.new(:a, :b), Foo.new(:c, :d), Foo.new(:a, :b)] > p x > p x.uniq > > ruby tst.rb > [#, #, > #] > [#, #, > #] > > I tried defining eq? and hash and uniq still fails. hash returns > identical values for objects with identical content and eq? returns > true in this case, but uniq does not remove them. harp:~ > cat a.rb class Foo ATTRIBUTES = %w( a b ) ATTRIBUTES.each{|at| attr at} def initialize(a,b) @a, @b = a, b end def parts() ATTRIBUTES.map{|at| send at} end def eql?(other) parts == other.parts end def hash() parts.hash end end p [ Foo.new(:a, :b), Foo.new(:c, :d), Foo.new(:a, :b) ] p [ Foo.new(:a, :b), Foo.new(:c, :d), Foo.new(:a, :b) ].uniq harp:~ > ruby a.rb [#, #, #] [#, #] -a -- my religion is very simple. my religion is kindness. -- the dalai lama