Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我的 Ruby 1.9.3 代码打开一个文件并检查每一行是否存在某个字符串:
if File.open('Gemfile').lines.any?{|line| line.include?('pg')} puts "found 'pg'" end
Ruby 2.0.0 给了我一个警告:
警告:不推荐使用 IO#lines;改用#each_line
在 Ruby 2.0.0 中实现这一点的最有效方法是什么?
在 ruby 2.0IO#each_line中可以返回一个枚举器。因此,替换lines为each_line或简单地替换each应该与您当前的代码完全相同。
IO#each_line
lines
each_line
each
if File.open('Gemfile').each {|line| line.include?('pg')} puts "found 'pg'" end