What’s the best Ruby idiom for reading data from a data file one line at a time
so that I could run the program with something like:
my_app.rb < data.txt
Thanks very much!
Christopher J. Meisenzahl CPS, CSTE
Senior Software Testing Consultant
Spherion
christopher.j.meisenzahl@citicorp.com
I have read that a good way is using ARGF.
ARGF.each do |line|
# do something
end
with the program run as you wrote it:
my_app.rb<data.txt
···
On Thursday, December 5, 2002, at 11:34 AM, christopher.j.meisenzahl@citicorp.com wrote:
What’s the best Ruby idiom for reading data from a data file one line
at a time
so that I could run the program with something like:
my_app.rb < data.txt
Thanks very much!
Christopher J. Meisenzahl CPS, CSTE
Senior Software Testing Consultant
Spherion
christopher.j.meisenzahl@citicorp.com
Hi,
What’s the best Ruby idiom for reading data from a data file one line at a time
so that I could run the program with something like:
my_app.rb < data.txt
A way to read lines from STDIN is:
while(line = gets)
do something with line
end
Or, to read lines from a particular file by name:
IO.foreach(“data.txt”) do |line|
do something with line
end
Hope this helps,
Bill
···
From: christopher.j.meisenzahl@citicorp.com