So I found this question on here, but I’m having an issue with the output and how to handle it with an if statement. This is what I have, but it’s always saying that it’s true even if the word monitor does not exist in the file
if File.readlines("testfile.txt").grep(/monitor/)
do something
end
Should it be something like == “nil”? I’m quite new to ruby and not sure of what the outputs would be.
I would use:
or
Using
readlineshas scalability issues though as it reads the entire file into an array. Instead, usingforeachwill accomplish the same thing without the scalability problem:or
See “Why is "slurping" a file not a good practice?” for more information about the scalability issues.