Detecting the end of a text file...

Discussion in 'Ruby' started by sketchitup@gmail.com, Jul 25, 2007.

  1. Guest

    I'm just beginning to learn the Ruby progamming language, although I
    have some previous programming experience in Java. I would like to
    know if how I can detect the end of a file when parsing a text file in
    Ruby. I don't want to throw an error or exception when I reach the end
    of the file, I just want to tell my parser to stop parsing.

    Thanks for the help.

    The SketchUp Artist
    , Jul 25, 2007
    #1
    1. Advertising

  2. On 7/25/07, <> wrote:
    > I'm just beginning to learn the Ruby progamming language, although I
    > have some previous programming experience in Java. I would like to
    > know if how I can detect the end of a file when parsing a text file in
    > Ruby. I don't want to throw an error or exception when I reach the end
    > of the file, I just want to tell my parser to stop parsing.


    As far as I know, all the IO methods return nil on EOF except for
    readline. You could read the file with:

    File.open(path) do |f|
    while line=f.gets
    # parse
    end
    end

    or

    File.open(path) do |f|
    begin
    loop do
    line = f.readline
    # parse
    end
    rescue EOFError
    end

    or slurp the whole file with IO.readline(path)

    --
    Luis Parravicini
    http://ktulu.com.ar/blog/
    Luis Parravicini, Jul 25, 2007
    #2
    1. Advertising

  3. Guest

    On Jul 25, 10:02 am, "Luis Parravicini" <> wrote:
    > On 7/25/07, <> wrote:
    >
    > > I'm just beginning to learn the Ruby progamming language, although I
    > > have some previous programming experience in Java. I would like to
    > > know if how I can detect the end of a file when parsing a text file in
    > > Ruby. I don't want to throw an error or exception when I reach the end
    > > of the file, I just want to tell my parser to stop parsing.

    >
    > As far as I know, all the IO methods return nil on EOF except for
    > readline. You could read the file with:
    >
    > File.open(path) do |f|
    > while line=f.gets
    > # parse
    > end
    > end
    >
    > or
    >
    > File.open(path) do |f|
    > begin
    > loop do
    > line = f.readline
    > # parse
    > end
    > rescue EOFError
    > end
    >
    > or slurp the whole file with IO.readline(path)
    >
    > --
    > Luis Parravicinihttp://ktulu.com.ar/blog/


    Thanks for the responses Luis. I think I need to read a little more
    about errors and exceptions in Ruby before I use your examples.

    Thanks,

    The SketchUp Artist
    , Jul 25, 2007
    #3
    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. Gary Thorpe

    Detecting end of file for VHDL'93

    Gary Thorpe, Jul 11, 2005, in forum: VHDL
    Replies:
    2
    Views:
    2,703
    Nicolas Matringe
    Jul 12, 2005
  2. walterbyrd
    Replies:
    7
    Views:
    311
    Asun Friere
    May 17, 2007
  3. Jimmy
    Replies:
    3
    Views:
    328
    J. J. Farrell
    Sep 9, 2011
  4. Jimmy
    Replies:
    13
    Views:
    651
    Peter Nilsson
    Sep 21, 2011
  5. Christopher R

    Detecting End of Download?

    Christopher R, Oct 29, 2003, in forum: Javascript
    Replies:
    0
    Views:
    134
    Christopher R
    Oct 29, 2003
Loading...

Share This Page