2015/10/27

自作クラスの配列同士で引き算を正しく動作させるには

class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def ==(other)
@x == other.x && @y == other.y
end
def to_s
"(#{x}, #{y})"
end
def inspect
to_s
end
end
p [Point.new(1, 1), Point.new(2, 2), Point.new(3, 3)] - [Point.new(3, 3), Point.new(1, 1)]
# => [(1, 1), (2, 2), (3, 3)]
class Point
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
def ==(other)
eql?(other)
end
def eql?(other)
@x == other.x && @y == other.y
end
def hash
@x + @y
end
def to_s
"(#{x}, #{y})"
end
def inspect
to_s
end
end
p [Point.new(1, 1), Point.new(2, 2), Point.new(3, 3)] - [Point.new(3, 3), Point.new(1, 1)]
# => [(2, 2)]
自作の配列で引き算を行おうとしたら、==のオーバーライドだけでは足りず、eql?hashのオーバーライドが必要だった。

0 件のコメント :

コメントを投稿