[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

IO vs. File

Ian Amuhton

1/9/2006 8:33:00 PM

Are the following two statements identical?

str = IO.read("test.txt")
str = File.read("test.txt")

I.e., do they both open the file for reading, slurp it into the string,
and close it?

They *appear* to be identical, judging from testing, but the docs are
unclear to me on this. E.g., the page on the IO class, which says ...

Many of the examples in this section use class File,
the only standard subclass of IO. The two classes are
closely associated.

...., shows IO.read, yet the page on the File class does not have a
File.read.

Also, there doesn't appear a similarly simple File.write that does all
the dirty work for you. The most concise I've come up with is:

File.open("test.html", "wb") { |f| f.write(str) }

Am I missing something?

--
da
~~


5 Answers

Erik Veenstra

1/9/2006 8:48:00 PM

0

> ..., shows IO.read, yet the page on the File class does not
> have a File.read.

File::read doesn't exist. IO::read does. But since File is a
subclass of IO, you can do File::read, which actually is
IO::read.

> Also, there doesn't appear a similarly simple File.write that
> does all the dirty work for you.

True.

gegroet,
Erik V. - http://www.erikve...

Ian Amuhton

1/9/2006 11:14:00 PM

0

Erik Veenstra wrote:
>> ..., shows IO.read, yet the page on the File class does not
>> have a File.read.

> File::read doesn't exist. IO::read does. But since File is a
> subclass of IO, you can do File::read, which actually is
> IO::read.

Ah, now I understand. Thank you.


--
da
~~


Robert Klemme

1/10/2006 8:28:00 AM

0

Donkey Agony wrote:
> Also, there doesn't appear a similarly simple File.write that does all
> the dirty work for you. The most concise I've come up with is:
>
> File.open("test.html", "wb") { |f| f.write(str) }
>
> Am I missing something?

No. I think this is on purpose because writing is more dangerous than
reading. :-)

robert

Gavin Sinclair

1/10/2006 9:55:00 AM

0

Donkey Agony wrote:

> Also, there doesn't appear a similarly simple File.write that does all
> the dirty work for you. The most concise I've come up with is:
>
> File.open("test.html", "wb") { |f| f.write(str) }
>
> Am I missing something?

No, and I agree it's a nuisance. Matz doesn't consider it to be a
common enough activity to warrant a method, last I heard. I define
File.write myself and use it frequently.

See http://extensions.rub... or the core/facets project (which
is more current in its maintenance).

Gavin

Ian Amuhton

1/11/2006 2:40:00 AM

0

Gavin Sinclair wrote:
>> Also, there doesn't appear a similarly simple File.write that does
>> all the dirty work for you. The most concise I've come up with is:
>>
>> File.open("test.html", "wb") { |f| f.write(str) }
>>
>> Am I missing something?
>
> No, and I agree it's a nuisance. Matz doesn't consider it to be a
> common enough activity to warrant a method, last I heard. I define
> File.write myself and use it frequently.
>
> See http://extensions.rub... or the core/facets project (which
> is more current in its maintenance).

Wow, bookmarked. Thanks for the reference, I'll certainly use some of
those!


--
da
~~