如果我有两个数组a
and 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
好的?
然后我从不同的部分填充了我的两个数组,我想得到不同。
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
andb
被保存在数组中。我想知道我需要在我的类中重写什么方法才能使其正常工作。