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:
That's you modifying underlying collection while iterating over it.
Basically, if collection changes in some way during iteration (becomes empty, gets prepended with a new element, etc), iterator doesn't have a lot of ways to handle it.
Try reject instead.
list.reject! {|item| item.name =~ /cat|dog|rat/i }
There is another way to do the same as reject!
with delete
:
list.delete_if { |item| item =~ /cat|dog|rat/i }