As others pointed out, you need to override #==in your class. However, one of them is hash tables. If you want two different instances of your class with o1 == o2 #=> truea hash to be equal to the same value in the hash table, you need to override #hashand #eql?let the hash table know that they represent the same value.
class Foo
def initialize(x,y,z)
@x,@y,@z = x,y,z
end
def ==(other)
@y == other.instance_eval { @y }
end
end
o1 = Foo.new(0, :frog, 2)
o2 = Foo.new(1, :frog, 3)
o1 == o2
h1 = Hash.new
h1[o1] = :jump
h1[o2]
class Foo
def hash
@y.hash
end
def eql?(other)
self == other
end
end
h2 = Hash.new
h2[o1] = :jump_again
h2[o2]
source
share