From: Radek Skokan Date: 2006-04-11T20:39:28+09:00 Subject: Re: Duplicity in Set ------=_Part_3802_2404317.1144755565143 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Thanks Daniel, I omitted the eql?. Now it works. Radek On 4/11/06, Daniel Harple wrote: > > On Apr 11, 2006, at 12:28 PM, Radek Skokan wrote: > > > I have a Set which should contains elements of my object Component. > > The > > component class implements method hash. I'm gathering data, createing > > Componets from them and storing it into the Set. It's a Set just to > > prevent > > duplicates. But finally the Set contains duplicate elemets -- with > > the same > > hash value. > > You must implement #eql? and #hash -- > > require 'set' > > class Component > attr_accessor :attributes > attr_reader :name, :subcomponents > def initialize(name) > @name =3D name > @attributes =3D {} > @subcomponents =3D [] > end > def inspect() %{<%s hash=3D%#x @name=3D%s>} % [self.class.name, > self.hash, @name.inspect] end > def eql?(other) @name.eql?(other.name) end > def hash() @name.hash end > end > > components =3D Set.new > components << Component.new("foo") > components << Component.new("bar") > components << Component.new("foo") > components.each { |c| puts c.inspect } > > -- Daniel > > ------=_Part_3802_2404317.1144755565143--