On Fri, 27 Aug 2004 22:57:53 +0900, James Edward Gray II
<> wrote:
> I have and Array and I need to iterate over it, finding a subset of the
> elements it contains. I want that subset in a new Array and I want the
> original Array altered so it no longer contains the subset.
>
> The following code does what I want, but it seems kind of "clunky":
>
> a = [ 1, 2, 3 ]
> b = [ ] # so it exists outside the iterator
>
> puts a.delete_if { |e| b << e if e > 1; e > 1; } # prints 1
> puts b # prints 2 and 3
It's still a bit clunky -- I can't think of a single operation to do
this offhand, but:
b = a.select { |e| e > 1 }
a.delete_if { |e| e > 1 }
-a
--
Austin Ziegler *
* Alternate: