------ art_81965_3775409.1157083609309 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Thanks, that works. I am wondering the requirement of #eql? is necessary. If the programmer wants to have non-equal objects to have same hash value, it is totally up to the programmer. On 8/31/06, Joel VanderWerf <vjoel / path.berkeley.edu> wrote: > > Jia Pu wrote: > > I have a class defined as: > > > > ########################### > > class MyClass > > def initialize(s1, s2, s3) > > @string1 1 > > @string2 2 > > @string3 3 > > end > > > > def hash > > return (@string1 + @string2 + @string3).hash > > end > > end > > ########################### > > > > Then I create two instances of this class: > > ########################### > > obj1 yClass.new('a', 'b', 'c') > > obj2 yClass.new('a', 'b', 'c') > > ########################### > > > > If verify obj1 and obj2 do have the same hash value, I check that > > 'obj1.hash obj2.hash' > > returns true > > > > Now i try to use those objects as hash key: > > h[obj1] abc' > > h[obj2] def' > > > > I expected h contains only one item with value 'def', since obj1 and > obj2 > > have the same hash value. But it turns out it has 2 items instead. > > > > Did I miss something? > > > > You need to define #eql? in your class, since that is how Hash compares > objects that hash the same: > > class MyClass > def contents > [@string1, @string2, @string3] > end > > def eql?(other) > other.class self.class and other.contents self.contents > end > end > > -- > vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 > > ------ art_81965_3775409.1157083609309--