I try to implement search function which looks for occurrence for particular keyword
, but if --max
options is provided it will print only some particular number of lines.
def search_in_file(path_to_file, keyword)
seen = false
File::open(path_to_file) do |f|
f.each_with_index do |line, i|
if line.include? keyword
# print path to file before only if there occurence of keyword in a file
unless seen
puts path_to_file.to_s.blue
seen = true
end
# print colored line
puts "#{i+1}:".bold.gray + "#{line}".sub(keyword, keyword.bg_red)
break if i == @opt[:max] # PROBLEM WITH THIS!!!
end
end
end
puts "" if seen
end
I try to use break
statement, but when it's within if ... end
block I can't break out from outer each_with_index
block.
If I move break
outside if ... end
it works, but it's not what I want.
How I can deal with this?
Thanks in advance.