Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/bade/ruby_extensions/array.rb

Instance Method Summary collapse

Instance Method Details

#rcount_matching(&block) ⇒ Fixnum

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

Returns:

  • count of items



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bade/ruby_extensions/array.rb', line 32

def rcount_matching(&block)
  count = 0

  reverse_each do |item|
    if block.call(item)
      count += 1
    else
      break
    end
  end

  count
end

#rindex_last_matching(&block) ⇒ Fixnum

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).

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bade/ruby_extensions/array.rb', line 10

def rindex_last_matching(&block)
  return nil if empty?

  index = nil

  current_index = count - 1
  reverse_each do |item|
    if block.call(item)
      index = current_index
      current_index -= 1
    else
      break
    end
  end

  index
end