I've come up with the following code that compares the IDs of the
objects (in the array) against one another and removes duplicates, but
this seems very kludgy and inefficient:
def dedupe
(0..@objects.length).each do |i|
if @objects[i]
((i+1)..@objects.length).each do |j|
if @objects[j]
if @objects[i].id == @objects[j].id
@objects[j] = nil
end
end
end
end
end
@objects.compact!
end
I know there's likely an easier and more-efficient way to do this, a
way to hijack uniq! into working with objects, perhaps. Could anyone
enlighten me?
Forgive the awful code, it's been a while since I've done much in Ruby
or read The Ruby Way. This project is meant as a refresher, but I'm
stuck here.
Thanks much,
Andrew
If you can, just fix the hash and/or eql? methods on the class to work
correctly with your definition of equality. Otherwise something like
your dedupe function is probably your best bet, since hashing isn't
going to work.
-- Brian
Thank you both,
Andrew