Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Is there a simpler way to do this?

Reply
Thread Tools

Is there a simpler way to do this?

 
 
Julian Leviston
Guest
Posts: n/a
 
      08-17-2005
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.


 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      08-17-2005
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


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

James Edward Gray II



 
Reply With Quote
 
 
 
 
Zach Dennis
Guest
Posts: n/a
 
      08-17-2005
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


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-17-2005
James Edward Gray II wrote:
> 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

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


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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-17-2005
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...


robert

 
Reply With Quote
 
astrodean@yahoo.co.uk
Guest
Posts: n/a
 
      08-17-2005
I believe the following does it...

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

Dean

 
Reply With Quote
 
Austin Ziegler
Guest
Posts: n/a
 
      08-17-2005
On 8/17/05, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> I believe the following does it...


> the_string=3DFile.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 =3D 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
--=20
Austin Ziegler * (E-Mail Removed)
* Alternate: (E-Mail Removed)


 
Reply With Quote
 
Frank Wallingford
Guest
Posts: n/a
 
      08-25-2005
> 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

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      08-25-2005

Frank Wallingford wrote:
> > 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?


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")

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      08-26-2005

William James wrote:
> Frank Wallingford wrote:
> > > 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?

>
> 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")


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a simpler way to modify all arguments in a function beforeusing the arguments? bruceg113355@gmail.com Python 20 11-16-2012 09:30 PM
Is there a simpler way to do this? Ritchie Valens Javascript 6 08-16-2011 09:23 PM
Is there a better/simpler way to filter blank lines? tmallen Python 19 11-05-2008 11:07 PM
Is there a simpler way (trick) to create composite controls ? Alex Nitulescu ASP .Net 5 03-03-2005 11:17 AM
super. could there be a simpler super? Kerim Borchaev Python 4 01-15-2004 03:15 PM



Advertisments