instance method extract!

Ruby on Rails 7.2.3

Since v6.0.6

Available in: v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

extract!()

Removes and returns the elements for which the block returns a true value. If no block is given, an Enumerator is returned instead.

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
odd_numbers = numbers.extract! { |number| number.odd? } # => [1, 3, 5, 7, 9]
numbers # => [0, 2, 4, 6, 8]
Source
# File activesupport/lib/active_support/core_ext/array/extract.rb, line 10
  def extract!
    return to_enum(:extract!) { size } unless block_given?

    extracted_elements = []

    reject! do |element|
      extracted_elements << element if yield(element)
    end

    extracted_elements
  end

Defined in activesupport/lib/active_support/core_ext/array/extract.rb line 10 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Array

Type at least 2 characters to search.

↑↓ navigate · open · esc close