J
James Edward Gray II
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
Is this the best way to go about it?
Thanks.
James Edward Gray II
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
Is this the best way to go about it?
Thanks.
James Edward Gray II