Delete from Array and return deleted elements in Ruby
If you don't need to retain the object id of a
:
a = [1,2,3,4,5,6,7,8,9,10]b, a = a.partition{|e| e < 4}b # => [1, 2, 3]a # => [4, 5, 6, 7, 8, 9, 10]
If you do need to retain the object id of a
, then use a temporal array c
:
a = [1,2,3,4,5,6,7,8,9,10]b, c = a.partition{|e| e < 4}a.replace(c)