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
Post a Comment