class Array

Public Instance Methods

rcount_matching() { |item| ... } click to toggle source

Returns count of items that matches, iteration starts at the end and stops on first not matching item.

@return [Fixnum] count of items

# File lib/bade/ruby_extensions/array.rb, line 30
def rcount_matching
  count = 0

  reverse_each do |item|
    break unless yield item

    count += 1
  end

  count
end
rindex_last_matching() { |item| ... } click to toggle source

Returns index of last matching item when iterating from back to start of self.

Returns nil when the first item does not match (when iterating from back).

@return [Fixnum]

# File lib/bade/ruby_extensions/array.rb, line 10
def rindex_last_matching
  return nil if empty?

  index = nil

  current_index = count - 1
  reverse_each do |item|
    break unless yield item

    index = current_index
    current_index -= 1
  end

  index
end