Print all lines containing the word "target" : IO.foreach « File Directory « Ruby






Print all lines containing the word "target"


IO.foreach("somefile") do |line|
  puts line if line =~ /target/
end

# Another way...
file = File.new("somefile")
file.each do |line|
  puts line if line =~ /target/
end

 








Related examples in the same category

1.Read lines one at a time and initialize a hash