Jeff Schwab
4/25/2006 1:00:00 AM
Ronny wrote:
> To process a file line by line, I use the following idiom:
>
> filename=....
> ....
> File.new(filename).each_line {|line| ..... }
>
> How can I use this approach, when the "file" to be processed, is the
> standard input of
> my Ruby program?
>
> (In case you know Perl: In Perl, I would set
> $filename="-"
> because the dash denotes the "special file" standard input in Perl, but
> this does not
> work in Ruby).
ARGF will represent either all files named on the command line, in
order, or standard input if no files are specified. E.g:
cmd> type main.rb
ARGF.each do |line|
puts line.upcase
end
cmd> ruby -w main.rb main.rb main.rb
ARGF.EACH DO |LINE|
PUTS LINE.UPCASE
END
ARGF.EACH DO |LINE|
PUTS LINE.UPCASE
END
cmd> ruby -w main.rb
hello world
HELLO WORLD
^Z
cmd>