If I have two arrays a
and b
, what method should the object contained have to override so the subtract method -
works properly?
Is it enough with eql?
EDIT
I'm adding more detail to my question.
I have this class defined:
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?
Then I have filled my two arrays from different parts and I want to get the difference.
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
But this is not working, because a
and b
are being kept in the array. I'm wondering what method to I need to override in my class for this to work properly.