On 4/5/06, Nathan Olberding <nathan.olberding / gmail.com> wrote:
> I have two arrays: originalList and deleteList. I'm trying to iterate
> through original list and remove each item if it appears in the
> deleteList. Simple enough. Here's my snippet for doing this:
>
> originalList.each do |item|
>         if deleteList.include?(item)
>                 puts "Deleting " + item
>                 originalList.delete(item)
>         end
> end
>
> Unfortunately, this code doesn't seem to be doing what I think it
> should. It seems to skip some items in originalList. I can provide
> examples upon request. Is there anything wrong with my algorithm?

I got bit by something similar this past weekend. It turns out that
Array#delete deletes based on == comparisons (generally means two
objects are from the same class and have the same instance variable
values), not eql? comparisons (means same object) as I had guessed.
That can be a significant difference. For one thing it allows the
delete method to delete more than one object from the array. If you
want to delete a specific object based on its identity, as far as I
know you need to do it like this.

obj_to_delete = whatever
my_array.delete_if { |obj| obj.eql?(obj_to_delete) }

--
R. Mark Volkmann
Object Computing, Inc.