I want to find further matches after Array#find_index { |item| block }
matches for the first time. How can I search for the index of the second match, third match, and so on?
In other words, I want the equivalent of the pos
argument to Regexp#match(str, pos)
for Array#find_index
. Then I can maintain a current-position index to continue the search.
I cannot use Enumerable#find_all
because I might modify the array between calls (in which case, I will also adjust my current-position index to reflect the modifications). I do not want to copy part of the array, as that would increase the computational complexity of my algorithm. I want to do this without copying the array:
new_pos = pos + array[pos..-1].find_index do |elem|
elem.matches_condition?
end
The following are different questions. They only ask the first match in the array, plus one:
- https://stackoverflow.com/questions/11300886/ruby-how-to-find-the-next-match-in-an-array
- https://stackoverflow.com/questions/4596517/ruby-find-next-in-array
The following question is closer, but still does not help me, because I need to process the first match before continuing to the next (and this way also conflicts with modification):