Jia Pu wrote: > I have a class defined as: > > ########################### > class MyClass > def initialize(s1, s2, s3) > @string1 = s1 > @string2 = s2 > @string3 = s3 > end > > def hash > return (@string1 + @string2 + @string3).hash > end > end > ########################### > > Then I create two instances of this class: > ########################### > obj1 = MyClass.new('a', 'b', 'c') > obj2 = MyClass.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