Sha256: 7721c52619201e3219183c0f177dbe4043ac70b477b72712e9de55042fc3fdb2

Contents?: true

Size: 529 Bytes

Versions: 1

Compression:

Stored size: 529 Bytes

Contents

unless Enumerable.method_defined? :take_last_while?
  module Enumerable
    # Take the last n elements of an enumerable meeting a certain predicate.
    #
    # @return [Array] an array containing the matching elements
    #
    # @example
    #   [1, 2, 3, 5].take_last_while(&:odd?) #=> [5, 3]
    def take_last_while
      return to_enum(:take_last_while) unless block_given?

      array = []
      reverse_each do |elem|
        return array unless yield(elem)
        array << elem
      end

      array
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
powerpack-0.0.6 lib/powerpack/enumerable/take_last_while.rb