Processing standard input with each_line

Discussion in 'Ruby' started by Ronny, Apr 24, 2006.

  1. Ronny

    Ronny Guest

    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
     
    Ronny, Apr 24, 2006
    #1
    1. Advertising

  2. Ronny

    Xavier Guest

    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
     
    Xavier, Apr 24, 2006
    #2
    1. Advertising

  3. 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
     
    Robert Klemme, Apr 24, 2006
    #3
  4. Ronny

    Dave Burt Guest

    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
     
    Dave Burt, Apr 24, 2006
    #4
  5. Ronny

    Ronny Guest

    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
     
    Ronny, Apr 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hubert Hung-Hsien Chang
    Replies:
    2
    Views:
    514
    Michael Foord
    Sep 17, 2004
  2. steve.leach

    How standard is the standard library?

    steve.leach, Apr 18, 2005, in forum: Python
    Replies:
    1
    Views:
    393
    Christos TZOTZIOY Georgiou
    Apr 18, 2005
  3. funkyj
    Replies:
    5
    Views:
    1,132
    funkyj
    Jan 20, 2006
  4. Robert Kern
    Replies:
    4
    Views:
    289
    Robert Kern
    Dec 29, 2007
  5. Jayson Williams

    Can you terminate an each_line statement.

    Jayson Williams, Oct 14, 2007, in forum: Ruby
    Replies:
    5
    Views:
    95
    Jayson Williams
    Oct 14, 2007
Loading...

Share This Page