Deleting items from an array requires multiple passes to remove them all

I have an array of ~1200 ruby objects and I want to loop over them and delete the ones with names that contain words or parts of words.

So I tried this:

list.each do |item|
  if item.name =~ /cat|dog|rat/i
    puts item.name
    list.delete(item)
  end
end

It works, except that it seems to miss some items with names that should match. If I run it again it finds a few more, and if I run it another time it finds a few more. It finds less each time, but I have to run it 3 times in order to delete everything.

Why in the world is this happening?

13
задан Phrogz 11 April 2011 в 03:35
поделиться