[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Processing standard input with each_line

Ronald Otto Valentin

4/24/2006 9:12:00 AM

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).

Ronald

5 Answers

Xavier

4/24/2006 10:07:00 AM

0

On Mon, 24 Apr 2006 02:11:58 -0700, 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?

STDIN.each_line{|l| ....}


Hth

Robert Klemme

4/24/2006 11:28:00 AM

0

Ronny wrote:
> To process a file line by line, I use the following idiom:
>
> filename=....
> ...
> File.new(filename).each_line {|line| ..... }

You should really use the block form because your code does not close
the file descriptor properly.

File.open(filename) {|io| io.each_line {|line| ... } }

However there's an even simpler approach:

File.foreach(filename) {|line| ... }

Kind regards

robert

Dave Burt

4/24/2006 3:24:00 PM

0

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?

STDIN aka $stdin is an IO object, just like your File.new(foo). Hence
(using the block form to ensure the file is closed):

File.open(filename) {|f| f.each_line {|line| ... } }
IO.foreach(filename) {|line| ... }

IO.open(0) {|f| f.each_line {|line| ... } }
STDIN.each_line {|line| ... }

See http://www.ruby-doc.org/core/class...

Cheers,
Dave

Jeff Schwab

4/25/2006 1:00:00 AM

0

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>

Ronald Otto Valentin

4/25/2006 7:53:00 AM

0


Dave Burt schrieb:
> STDIN aka $stdin is an IO object, just like your File.new(foo). Hence
> (using the block form to ensure the file is closed):
>
> File.open(filename) {|f| f.each_line {|line| ... } }
> IO.foreach(filename) {|line| ... }
>
> IO.open(0) {|f| f.each_line {|line| ... } }
> STDIN.each_line {|line| ... }

Thank you, that's it!!!!

Ronald