M
Michel Demazure
I want to check whether a given array contains a given subarray and
return the corresponding range.
Is there a builtin method ? If not, do you have better than this :
class Array
def look_up(sub_array)
len = sub_array.size
self.each_cons(len).with_index do |cons, index|
return Range.new(index, index + len - 1) if cons == sub_array
end
nil
end
end
_md
return the corresponding range.
Is there a builtin method ? If not, do you have better than this :
class Array
def look_up(sub_array)
len = sub_array.size
self.each_cons(len).with_index do |cons, index|
return Range.new(index, index + len - 1) if cons == sub_array
end
nil
end
end
_md