IO vs. File

D

Donkey Agony

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?
 
E

Erik Veenstra

..., 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.erikveen.dds.nl/
 
D

Donkey Agony

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.
 
R

Robert Klemme

Donkey said:
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
 
G

Gavin Sinclair

Donkey said:
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.rubyforge.org or the core/facets project (which
is more current in its maintenance).

Gavin
 
D

Donkey Agony

Gavin said:
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.rubyforge.org 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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top