How to remove elements of array in place returning the removed elements
arrays, ruby
Solution
I'd use `partition` here. It doesn't modify `self` inplace, but returns two new arrays. By assigning the second array to `arr` again, it gets the results you want:
comp_arr, arr = arr.partition { |a| a > 1 }
See the documentation of partition.
Problem
I have an array `arr`. I want to destructively remove elements from `arr` based on a condition, returning the removed elements. ``` arr = [1,2,3] arr.some_method{|a| a > 1} #=> [2, 3] arr #=> [1] ``` My first try was `reject!`: ``` arr = [1,2,3] arr.reject!{|a| a > 1} ``` but the returning blocks and `arr`'s value are both `[1]`. I could write a custom function, but I think there is an explicit method for this. What would that be? Update after the question was answered: `partition` method turns out to be useful for implementing this behavior for hash as well. How can I remove elements of a hash, returning the removed elements and the modified hash? ``` hash = {:x => 1, :y => 2, :z => 3} comp_hash, hash = hash.partition{|k,v| v > 1}.map{|a| Hash[a]} comp_hash #=> {:y=>2, :z=>3} hash #=> {:x=>1} ```