Get the content of a file

Discussion in 'Ruby' started by Dirk Einecke, Jul 25, 2004.

  1. Dirk Einecke

    Dirk Einecke Guest

    Hi.

    I've a method to read the content of a file.

    def readFile(path)
    value = ''
    file = File.open(path, File::RDONLY)
    while line = file.gets do
    value += line
    end
    file.close
    return value
    end

    I think the way line by line is not very efficiently.
    Is there a shorter way to get the content of a file?

    greetings
    Dirk Einecke
     
    Dirk Einecke, Jul 25, 2004
    #1
    1. Advertising

  2. Dirk Einecke wrote:
    > Is there a shorter way to get the content of a file?


    File.read(filename)
     
    Andreas Schwarz, Jul 25, 2004
    #2
    1. Advertising

  3. Dirk Einecke

    ts Guest

    >>>>> "D" == Dirk Einecke <> writes:

    D> Is there a shorter way to get the content of a file?


    svg% ri IO::read
    --------------------------------------------------------------- IO::read
    IO.read(rane, [length [, offset]] ) => string
    ------------------------------------------------------------------------
    Opens the file, optionally seeks to the given offset, then returns
    _length_ bytes (defaulting to the rest of the file). +read+ ensures
    the file is closed before returning.

    IO.read("testfile") #=> "This is line one\nThis is line two\
    nThis is line three\nAnd so on...\n"
    IO.read("testfile", 20) #=> "This is line one\nThi"
    IO.read("testfile", 20, 10) #=> "ne one\nThis is line "

    svg%



    Guy Decoux
     
    ts, Jul 25, 2004
    #3
  4. Dirk Einecke

    Gully Foyle Guest

    Dirk Einecke wrote:

    > Hi.
    >
    > I've a method to read the content of a file.
    >
    > def readFile(path)
    > value = ''
    > file = File.open(path, File::RDONLY)
    > while line = file.gets do
    > value += line
    > end
    > file.close
    > return value
    > end
    >
    > I think the way line by line is not very efficiently.
    > Is there a shorter way to get the content of a file?
    >
    > greetings
    > Dirk Einecke


    Simple way is:

    content = File.read(filename)

    But if you should learn about blocks so the code you provided can be
    replaced with something like the following (because your code doesn't
    have exception handling to guarantee that file.close is called):

    File.open(path, File::RDONLY){|file|
    #use file.gets or whatever in here, preferably using another block
    }
     
    Gully Foyle, Jul 25, 2004
    #4
  5. Dirk Einecke

    Dirk Einecke Guest

    Hi.

    Andreas Schwarz wrote:
    > Dirk Einecke wrote:
    >> Is there a shorter way to get the content of a file?

    >
    > File.read(filename)


    That's not working and I can not find read() here:
    http://www.rubycentral.com/ref/ref_c_file.html

    I'm using Ruby 1.6.7

    greetings
    Dirk Einecke
     
    Dirk Einecke, Aug 1, 2004
    #5
  6. Dirk Einecke wrote:

    > Hi.
    >
    > Andreas Schwarz wrote:
    >
    >> Dirk Einecke wrote:
    >>
    >>> Is there a shorter way to get the content of a file?

    >>
    >>
    >> File.read(filename)

    >
    >
    > That's not working and I can not find read() here:
    > http://www.rubycentral.com/ref/ref_c_file.html
    >
    > I'm using Ruby 1.6.7
    >
    > greetings
    > Dirk Einecke
    >

    Look here http://www.rubycentral.com/ref/ref_c_io.html#read . You are
    dealing with an IO object (the file) so you can use the IO methods.

    You can do something like
    aFile = File.new("#{textfilename}", "r")
    contentsoffile = aFile.read

    -Matthew Margolis
     
    Matthew Margolis, Aug 1, 2004
    #6
  7. Dirk Einecke

    Kent Sibilev Guest

    I use the following idiom to read the whole file:

    data = open(path) {|f| f.read}

    Cheers,
    Kent.

    On Jul 25, 2004, at 4:56 AM, Dirk Einecke wrote:

    > Hi.
    >
    > I've a method to read the content of a file.
    >
    > def readFile(path)
    > value = ''
    > file = File.open(path, File::RDONLY)
    > while line = file.gets do
    > value += line
    > end
    > file.close
    > return value
    > end
    >
    > I think the way line by line is not very efficiently.
    > Is there a shorter way to get the content of a file?
    >
    > greetings
    > Dirk Einecke
    >
     
    Kent Sibilev, Aug 1, 2004
    #7
  8. Dirk Einecke wrote:
    > Hi.
    >
    > Andreas Schwarz wrote:
    >
    >> Dirk Einecke wrote:
    >>
    >>> Is there a shorter way to get the content of a file?

    >>
    >>
    >> File.read(filename)

    >
    >
    > That's not working and I can not find read() here:
    > http://www.rubycentral.com/ref/ref_c_file.html
    >
    > I'm using Ruby 1.6.7


    What I did in Ruby < 1.8 is the following:

    File.readlines(filename).to_s

    or instead of to_s, join("")

    Regards,

    Michael
     
    Michael Neumann, Aug 1, 2004
    #8
  9. Dirk Einecke

    Dirk Einecke Guest

    Hi.

    Well - thanks to all. Now I've several ways:

    1.
    value = open(path) {|f| f.read}

    2.
    file = File.open(path, File::RDONLY)
    value = file.read
    file.close

    3.
    value = File.readlines(path).to_s

    4.
    value = File.readlines(path).join

    What is the fastes way (for Ruby) and what should I use?

    greetings
    Dirk Einecke
     
    Dirk Einecke, Aug 1, 2004
    #9
  10. Dirk Einecke <> writes:

    > Hi.
    >
    > Well - thanks to all. Now I've several ways:
    >
    > 1.
    > value = open(path) {|f| f.read}
    >
    > 2.
    > file = File.open(path, File::RDONLY)
    > value = file.read
    > file.close
    >
    > 3.
    > value = File.readlines(path).to_s
    >
    > 4.
    > value = File.readlines(path).join
    >
    > What is the fastes way (for Ruby) and what should I use?
    >
    > greetings
    > Dirk Einecke


    In my opinion, number one is the best. The latter two only make sense
    for line-based formats, and even in such cases it's easier to just read
    it all in at once. Number two, I assume, is just like number one,
    except you have to close it up yourself, which is annoying.

    mikael
     
    Mikael Brockman, Aug 1, 2004
    #10
  11. Dirk Einecke wrote:
    > Hi.
    >
    > Well - thanks to all. Now I've several ways:
    >
    > 1.
    > value = open(path) {|f| f.read}
    >
    > 2.
    > file = File.open(path, File::RDONLY)
    > value = file.read
    > file.close


    2b.

    value = File.open(path) {|f| f.read}

    > 3.
    > value = File.readlines(path).to_s
    >
    > 4.
    > value = File.readlines(path).join
    >
    > What is the fastes way (for Ruby) and what should I use?


    1. and 2. should be the fastest!


    Regards,

    Michael
     
    Michael Neumann, Aug 1, 2004
    #11
  12. Dirk Einecke

    Dirk Einecke Guest

    Hi.

    Michael Neumann wrote:
    > value = File.open(path) {|f| f.read}


    Okay - I take this one.

    greetings
    Dirk Einecke
     
    Dirk Einecke, Aug 1, 2004
    #12
  13. Dirk Einecke

    CT Guest

    What about,
    value = File.read(path)

    On Mon, 2 Aug 2004 02:46:38 +0900, Dirk Einecke <> wrote:
    > Hi.
    >
    > Michael Neumann wrote:
    > > value = File.open(path) {|f| f.read}

    >
    > Okay - I take this one.
    >
    > greetings
    > Dirk Einecke
    >
    >
     
    CT, Aug 6, 2004
    #13
    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. J.Edwards
    Replies:
    0
    Views:
    1,671
    J.Edwards
    Jan 20, 2006
  2. TheKeith
    Replies:
    20
    Views:
    106,641
    Chris Morris
    Oct 29, 2003
  3. hazz
    Replies:
    6
    Views:
    49,646
    SkyUCHC
    Jun 9, 2010
  4. Stanimir Stamenkov
    Replies:
    2
    Views:
    750
    Stanimir Stamenkov
    Oct 25, 2005
  5. Replies:
    0
    Views:
    726
Loading...

Share This Page