I am looking for a way to do the following in Ruby in a cleaner way:
class Array
def find_index_with_offset(offset, &block)
[offset..-1].find &block
end
end
offset = array.find_index {|element| element.meets_some_criterion?}
the_object_I_want =
array.find_index_with_offset(offset+1) {|element| element.meets_another_criterion?}
So, I look for a Ruby array for the index of an object, and then do a subsequent search to find the first object that matches another criterion and has a higher index in the array. Is there a better way to do this?
What I mean by pure: something that is not related to explicitly slicing an array. When you do this several times, the calculation of the cutting indices becomes messy. I would like to continue working with the original array. This is easier to understand and less error prone.
NB. In my actual code, I don't have a monkey processed array, but I want to pay attention to the fact that I expect that I will duplicate the existing Array / Enumerable functionality
Changes