2 つの配列a
とb
がある場合、減算メソッド-
が適切に機能するように、含まれているオブジェクトをオーバーライドする必要があるメソッドはどれですか?
で十分かeql?
編集
質問に詳細を追加しています。
私はこのクラスを定義しています:
class Instance
attr_reader :id, :desc
def initialize( id , desc )
@id = id.strip
@desc = desc.strip
end
def sameId?( other )
@id == other.id
end
def eql?( other )
sameId?( other ) and @desc == other.desc
end
def to_s()
"#{id}:#{desc}"
end
end
Ok?
次に、2 つの配列を異なる部分から埋めたので、違いを取得したいと思います。
a = Instance.new("1","asdasdad")
b = Instance.new("1","a")
c = Instance.new("1","a")
p a.eql?(b) #false
p b.eql?(c) #true
x = [a,b]
y = [c]
z = x - y # should leave a because b and c "represent" the same object
a
しかし、とb
が配列に保持されているため、これは機能しません。これが正しく機能するために、クラスでどのメソッドをオーバーライドする必要があるのか 疑問に思っています。