Extract array in place
class Array
def extract!
ary = self.dup
self.reject! { |x| yield x }
ary - self
end
end
p a = ("a".."h").to_a
p a.extract! { |x| x < "e" && x != "b" } # => ["a", "c", "d"]
p a # => ["b", "e", "f", "g", "h"]
Related examples in the same category