Ruby: how to find the next match in an array -


i have search item in array , return value of next item. example:

a = ['abc.df','-f','test.h'] = a.find_index{|x| x=~/-f/} puts a[i+1] 

is there better way other working index?

a classical functional approach uses no indexes (xs.each_cons(2) -> pairwise combinations of xs):

xs = ['abc.df', '-f', 'test.h'] (xs.each_cons(2).detect { |x, y| x =~ /-f/ } || []).last #=> "test.h" 

using enumerable#map_detect simplifies litte bit more:

xs.each_cons(2).map_detect { |x, y| y if x =~ /-f/ } #=> "test.h" 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -