Delete from Array and return deleted elements in Ruby -
how can delete elements array , 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 can use instead of foo.get_a.sth
?
if don't need retain object id of a
:
a = [1,2,3,4,5,6,7,8,9,10] b, = a.partition{|e| e < 4} b # => [1, 2, 3] # => [4, 5, 6, 7, 8, 9, 10]
if need retain object id of a
, use temporal array c
:
a = [1,2,3,4,5,6,7,8,9,10] b, c = a.partition{|e| e < 4} a.replace(c)
Comments
Post a Comment