Sha256: 47f4f0e7b642195693af9992e872b94fb39740a221007b8c4274739006e4fbbc

Contents?: true

Size: 577 Bytes

Versions: 204

Compression:

Stored size: 577 Bytes

Contents

# frozen_string_literal: true

class Array
  # 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]
  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
end

Version data entries

204 entries across 196 versions & 23 rubygems

Version Path
activesupport-8.0.0 lib/active_support/core_ext/array/extract.rb
activesupport-7.2.2 lib/active_support/core_ext/array/extract.rb
activesupport-7.1.5 lib/active_support/core_ext/array/extract.rb
activesupport-8.0.0.rc2 lib/active_support/core_ext/array/extract.rb
activesupport-7.2.1.2 lib/active_support/core_ext/array/extract.rb
activesupport-7.1.4.2 lib/active_support/core_ext/array/extract.rb
activesupport-7.0.8.6 lib/active_support/core_ext/array/extract.rb
activesupport-6.1.7.10 lib/active_support/core_ext/array/extract.rb
activesupport-8.0.0.rc1 lib/active_support/core_ext/array/extract.rb
activesupport-6.1.7.9 lib/active_support/core_ext/array/extract.rb
activesupport-7.2.1.1 lib/active_support/core_ext/array/extract.rb
activesupport-7.1.4.1 lib/active_support/core_ext/array/extract.rb
activesupport-7.0.8.5 lib/active_support/core_ext/array/extract.rb
logjam_agent-0.39.1 lib/logjam_agent/active_support/core_ext/array/extract.rb
activesupport-8.0.0.beta1 lib/active_support/core_ext/array/extract.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/core_ext/array/extract.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/core_ext/array/extract.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/core_ext/array/extract.rb
omg-activesupport-8.0.0.alpha4 lib/active_support/core_ext/array/extract.rb
omg-activesupport-8.0.0.alpha3 lib/active_support/core_ext/array/extract.rb