From: Alder Green Date: 2006-05-26T07:03:57+09:00 Subject: Array#delete_if! - a method to delete and return all array items for which the block evaluates to true. Here's a method to delete and return all items in an Array for which the block evaluates to true: class Array def delete_if! to_delete = [] to_keep = [] each do |item| (yield item) ? to_delete << item : to_keep << item end replace(to_keep) return to_delete end end Demonstration: irb(main):002:0> arr = (1..12).to_a [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] irb(main):003:0> arr.delete_if! {|i| (i % 3) == 0} [3, 6, 9, 12] irb(main):004:0> arr [1, 2, 4, 5, 7, 8, 10, 11] Would people suggest better implementations? The obvious problem here is that we're duplicating arr (to_delete and to_keep contatin together a cloned arr). I came up with an index based solution to avoid that, but frankly it was quite ugly. -- -Alder