所以我在这里发现了这个问题,但是我对输出有一个问题,以及如何用if语句处理它.这就是我所拥有的,但它总是说这是真的,即使文件中不存在monitor这个词
if File.readlines("testfile.txt").grep(/monitor/)
do something
end
它应该是像=="nil"这样的东西吗?我对ruby很陌生,不确定它的输出是什么.
所以我在这里发现了这个问题,但是我对输出有一个问题,以及如何用if语句处理它.这就是我所拥有的,但它总是说这是真的,即使文件中不存在monitor这个词
if File.readlines("testfile.txt").grep(/monitor/)
do something
end
它应该是像=="nil"这样的东西吗?我对ruby很陌生,不确定它的输出是什么.
我会使用:
if File.readlines("testfile.txt").grep(/monit或/).any?
或
if File.readlines("testfile.txt").any?{ |l| l['monit或'] }
Using readlines
has scalability issues though as it reads the entire file into an array. Instead, using f或each
will accomplish the same thing without the scalability problem:
if File.f或each("testfile.txt").grep(/monit或/).any?
或
if File.f或each("testfile.txt").any?{ |l| l['monit或'] }
See "Why is "slurping" a file not a good practice?" f或 m或e inf或mation about the scalability issues.