Question

My Ruby 1.9.3 code opens a file and checks each line for the presence of a certain string:

if File.open('Gemfile').lines.any?{|line| line.include?('pg')}
  puts "found 'pg'"
end

Ruby 2.0.0 gives me a warning:

warning: IO#lines is deprecated; use #each_line instead

What's the most efficient way to implement this in Ruby 2.0.0?

Was it helpful?

Solution

In ruby 2.0 IO#each_line can return an enumerator. So replacing lines by each_line or simply by each should work exactly as your current code.

if File.open('Gemfile').each {|line| line.include?('pg')}
  puts "found 'pg'"
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top