Use each method to loop through a text file : Text file « File Directory « Ruby






Use each method to loop through a text file


#!/usr/bin/env ruby

file = File.open( "myFile.txt" )

file.each { |line| print "#{file.lineno}. ", line }

file.close

 








Related examples in the same category

1.
2.Read line from a text file
3.Output each line in a file and close it
4.Use code block to insert string to a file
5.Read a text file with File.read
6.Read a text file readlines
7.Replace string in a file