I’m very new to ruby. I’m trying to search for any instance of a word in a text file (not the problem). Then when the word is discovered, it would show the surrounding text (maybe 3-4 words before and after the target word, instead of the whole line), output to a list of instances and continue searching.
Example:
The quick brown fox jumped over the lazy dog.
Search word: jumped
Output: …brown fox jumped over the…
Any help is appreciated.
def word_exists_in_file
f = File.open("test.txt")
f.each do line
print line
if line.match /someword/
return true
end
end
false
end
It’s not perfect solution, just the path, so work around it.