From: Eric Hodel Date: 2006-05-26T07:18:15+09:00 Subject: Re: Array#delete_if! - a method to delete and return all array items for which the block evaluates to true. On May 25, 2006, at 3:15 PM, Eric Hodel wrote: > On May 25, 2006, at 3:03 PM, Alder Green wrote: > >> Here's a method to delete and return all items in an Array for which >> the block evaluates to true: > > [...] > >> 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. > > $ ri Array#reject! Oops, I meant: $ ri partition --------------------------------------------------- Enumerable#partition enum.partition {| obj | block } => [ true_array, false_array ] ------------------------------------------------------------------------ Returns two arrays, the first containing the elements of _enum_ for which the block evaluates to true, the second containing the rest. (1..6).partition {|i| (i&1).zero?} #=> [[2, 4, 6], [1, 3, 5]] -- Eric Hodel - drbrain@segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com