As others have pointed out, you need to redefine #==
in your class. One gotcha, though, is hash tables. If you want two different instances of your class with o1 == o2 #=> true
to hash to the same value in a hash table, then you need to redefine #hash
and #eql?
so the hash table knows 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 #=> true
h1 = Hash.new
h1[o1] = :jump
h1[o2] #=> nil
class Foo
def hash
@y.hash
end
def eql?(other)
self == other
end
end
h2 = Hash.new
h2[o1] = :jump_again
h2[o2] #=> :jump_again