Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a simpler way to do this?

1 view
Skip to first unread message

Julian Leviston

unread,
Aug 17, 2005, 11:30:50 AM8/17/05
to
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

unread,
Aug 17, 2005, 11:32:26 AM8/17/05
to

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

James Edward Gray II

Zach Dennis

unread,
Aug 17, 2005, 11:37:12 AM8/17/05
to
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 Klemme

unread,
Aug 17, 2005, 11:39:20 AM8/17/05
to
James Edward Gray II wrote:

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

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. :-)

Kind regards

robert

Robert Klemme

unread,
Aug 17, 2005, 12:00:09 PM8/17/05
to

One less...

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

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

robert

astr...@yahoo.co.uk

unread,
Aug 17, 2005, 1:27:19 PM8/17/05
to
I believe the following does it...

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

Dean

Austin Ziegler

unread,
Aug 17, 2005, 2:58:04 PM8/17/05
to
On 8/17/05, astr...@yahoo.co.uk <astr...@yahoo.co.uk> wrote:
> I believe the following does it...

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

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
--
Austin Ziegler * halos...@gmail.com
* Alternate: aus...@halostatue.ca


Frank Wallingford

unread,
Aug 25, 2005, 1:41:43 PM8/25/05
to
> contents = open(filename, "rb") { |f| f.read }
>
> It's safe for text or binary files on all platforms.

Is this true?

I couldn't find an answer in my quick glance over the standard library
docs, but I was under the impression that if you open a text file in
binary mode on some platforms that do text-mode translation for you
(Windows, for example), then you lose the translation. You may get
"\r\n" instead of "\n" for newlines (since that's how they exist in
binary on disk) and you may get "^Z" for the end of file marker.

I don't have a Windows machine to test with at the moment, but wouldn't
you get those extra \r and ^Z characters if you opened a text file in
binary mode?

-Frank

William James

unread,
Aug 25, 2005, 4:43:28 PM8/25/05
to

Just tried it. You get the \r characters but not the ^Z.
The end of file marker was only needed in the days when the messy-dos
filesystem didn't record the actual size of the file's contents.

After using the command above, you would split contents into
lines using
contents.split("\r\n")

William James

unread,
Aug 26, 2005, 2:43:53 PM8/26/05
to

Since this is supposed to work on all platforms:
contents.split(/(?:\r\n?)|\n/)

francis...@hotmail.com

unread,
Aug 27, 2005, 3:11:56 PM8/27/05
to

> Since this is supposed to work on all platforms:
> contents.split(/(?:\r\n?)|\n/)

Can you explain this pattern please ? I don't understand the "?:" part.

Francis

William James

unread,
Aug 27, 2005, 3:59:41 PM8/27/05
to

In a regexp, ( ) groups and captures; (?: ) groups without capturing.

But I made it too complex. The group isn't necessary:

irb(main):002:0> "a\nb\nc\r\nd\r\ne\rf".split(/\r\n?|\n/)
=> ["a", "b", "c", "d", "e", "f"]

Serpent

unread,
Aug 27, 2005, 7:16:56 PM8/27/05
to
But my point is that reading text files in binary mode is bad. If you
want this to work on all platforms, open text files in text mode and
open binary files in binary mode.

0 new messages