"James Edward Gray II" <james / grayproductions.net> schrieb im Newsbeitrag
news:0AA1504B-F833-11D8-B4C8-000A95BA45F8 / grayproductions.net...
> On Aug 27, 2004, at 9:05 AM, ts wrote:
>
> >>>>>> "J" == James Edward Gray <james / grayproductions.net> writes:
> >
> > J> I have and Array and I need to iterate over it, finding a subset of
> > the
> > J> elements it contains.  I want that subset in a new Array and I want
> > the
> > J> original Array altered so it no longer contains the subset.
> >
> >  You can also use Enum#partition
> >
> >    a, b = a.partition { |e| e <= 1}
>
> Exactly what I was looking for.  Thank you.

Note, that partition returns two new arrays and leaves the original
unmodified.  So for large arrays that might be a problem.  That might or
might not be ok, depending on your situation.

Btw, your solution can be written a bit simpler:

a = [1,2,3]
b = []
a.delete_if {|e| e > 1 and b << e}

IMHO that is one of the shortest of the solutions that need only two arrays.

If you know that the array is sorted and that there is at least one element
that will satisfy the condition, more options are possible:

a = [1,2,3]
b = a.slice!(a.each_with_index {|x,i| break i if x > 1} .. -1)

Kind regards

    robert