Delete from Array and return deleted elements in Ruby
arrays, ruby, select
Solution
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)
Problem
How can I delete some elements from an array and select them? For example: ``` class Foo def initialize @a = [1,2,3,4,5,6,7,8,9] end def get_a return @a end end foo = Foo.new b = foo.get_a.sth{ |e| e < 4 } p b # => [1,2,3] p foo.get_a # => [4,5,6,7,8,9,10] ``` What I can use instead of `foo.get_a.sth`?