Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Processing standard input with each_line

Reply
Thread Tools

Processing standard input with each_line

 
 
Ronny
Guest
Posts: n/a
 
      04-24-2006
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

 
Reply With Quote
 
 
 
 
Xavier
Guest
Posts: n/a
 
      04-24-2006
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
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      04-24-2006
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
 
Reply With Quote
 
Dave Burt
Guest
Posts: n/a
 
      04-24-2006
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/classes/IO.html

Cheers,
Dave
 
Reply With Quote
 
Ronny
Guest
Posts: n/a
 
      04-25-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: parallel processing in standard library Robert Kern Python 4 12-29-2007 06:16 AM
Can you terminate an each_line statement. Jayson Williams Ruby 5 10-14-2007 02:36 AM
How standard is the standard library? steve.leach Python 1 04-18-2005 04:07 PM
Post-Processing RAW vs Post-Processing TIFF Mike Henley Digital Photography 42 01-30-2005 08:26 AM
Question: processing HTML, re-write default processing action of many tags Hubert Hung-Hsien Chang Python 2 09-17-2004 03:10 PM



Advertisments