From: jogloran Date: 2006-04-14T11:35:24+09:00 Subject: Re: Array#- ------=_Part_6307_15856866.1144982120863 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Ah, thank you. But could this change in future versions? The fact that the operation is backed by a hash operation is not mentioned in the documentation. Thanks On 4/14/06, Mauricio Fernandez wrote: > > On Thu, Apr 13, 2006 at 11:21:18PM +0900, jogloran wrote: > > I need items to be compared based on their values, and not by object > > equality, so I provide #=3D=3D for the array elements. The problem is t= hat > I've > > been trying to use Array#- as a set difference-like operation, and it > > doesn't seem to respect any kind of user-provided means of defining > > equality. A look into the Ruby source seems to indicate that it works b= y > > populating a hash with the elements of the second array, then appending > to > > the result array if an element of the first array is in the hash. Does > this > > mean that it is impossible to get the behaviour I want from Array#- as > it is > > implemented? > > You have to define #hash and #eql?: > > RUBY_VERSION # =3D> "1.8.4" > class X > attr_reader :foo > def initialize(foo); @foo =3D foo end > end > > [X.new(1), X.new(2)] - [X.new(1)] # =3D> [# @foo=3D1>, #] > > X.new(1).hash # =3D> -605071554 > X.new(1).hash # =3D> -605071594 > class X > def hash > @foo.hash > end > def eql?(x) > foo =3D=3D x.foo > end > end > > [X.new(1), X.new(2)] - [X.new(1)] # =3D> [# @foo=3D2>] > > -- > Mauricio Fernandez - http://eigenclass.org - singular Ruby > > ------=_Part_6307_15856866.1144982120863--