Is there a simpler way to do this?

(Julian Leviston) #1

Hi.

Just wondering if there's a simpler way to do this?

file = File.open("/usr/blah/1.txt") do | file |
     while line = file.gets
         the_string += line
     end
end

In other words, open a file, read the contents into a string called the_string

Thanks,
Julian.

(James Edward Gray II) #2

file_contents = File.read("/usr/blah/1.txt")

James Edward Gray II

···

On Aug 17, 2005, at 10:30 AM, Julian Leviston wrote:

Hi.

Just wondering if there's a simpler way to do this?

file = File.open("/usr/blah/1.txt") do | file |
    while line = file.gets
        the_string += line
    end
end

In other words, open a file, read the contents into a string called the_string

#3

I believe the following does it...

the_string=File.readlines("/usr/blah/1.txt"').join

Dean

(Zach Dennis) #4

James Edward Gray II wrote:

file_contents = File.read("/usr/blah/1.txt")

or, for two more characters shortened...

file_contents = IO.read("/usr/blah/1.txt")

Zach

(Robert) #5

James Edward Gray II wrote:

Hi.

Just wondering if there's a simpler way to do this?

file = File.open("/usr/blah/1.txt") do | file |
    while line = file.gets
        the_string += line
    end
end

In other words, open a file, read the contents into a string called
the_string

file_contents = File.read("/usr/blah/1.txt")

I was tempted to say "of course there is". It's so typical Ruby. :slight_smile:

Btw, a remark to the OP's algorithm: IMHO this one is more efficient since
it does not create new String objects all the time:

file = File.open("/usr/blah/1.txt") do | file |
     s = ""
     while line = file.gets
         s << line
     end
     s
end

Note also that you might get nil instead of the empty string in your
example if reading from an empty file (untested). But of course the one
liner is even more efficient. :slight_smile:

Kind regards

    robert

···

On Aug 17, 2005, at 10:30 AM, Julian Leviston wrote:

(Austin Ziegler) #6

Yes. But there's a problem with this -- it does more work than
necessary. The IO.read approach that is superior. However, I prefer:

  contents = open(filename, "rb") { |f| f.read }

It's not quite as short as the IO.read approach, but it does two things for me:

1. It's safe for text or binary files on all platforms.
2. Because I'm using "open", if I do "require 'open-uri'", then
filename can also be a URL to a remoate location.

-austin

···

On 8/17/05, astrodean@yahoo.co.uk <astrodean@yahoo.co.uk> wrote:

I believe the following does it...

the_string=File.readlines("/usr/blah/1.txt"').join

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

(Robert) #7

Zach Dennis wrote:

James Edward Gray II wrote:

file_contents = File.read("/usr/blah/1.txt")

or, for two more characters shortened...

file_contents = IO.read("/usr/blah/1.txt")

One less...

file_contents = IO.read "/usr/blah/1.txt"

Of course, you can also truncate the variable name...
:slight_smile:

    robert