Q: IO.read() and IO.write()

K

kwatch

Hi,

I have questions about IO and File class.

* Why is IO.read(filename) defined?
IMO, File.read() is more natural than IO.read() because
IO class is not related to filename, I think.

* Is there any reason that IO.write() (or File.write()) is not
provided?
I have to define File.write() for each project...
It is easy to define File.write() but I hope it is provided by Ruby.
 
R

Robert Klemme

2008/9/15 kwatch said:
Hi,

I have questions about IO and File class.

* Why is IO.read(filename) defined?

I don't know.
IMO, File.read() is more natural than IO.read() because
IO class is not related to filename, I think.

You can as well use File.read().
* Is there any reason that IO.write() (or File.write()) is not
provided?
I have to define File.write() for each project...

Why don't you just create a library of things you regularly need and
require that?
It is easy to define File.write() but I hope it is provided by Ruby.

Probably because writing is more dangerous and also there are more
options to file writing than to read (beginning vs. at end, create or
not, text vs. binary).

Kind regards

robert
 
D

Doug Glidden

kwatch said:
Hi,

I have questions about IO and File class.

* Why is IO.read(filename) defined?
IMO, File.read() is more natural than IO.read() because
IO class is not related to filename, I think.

* Is there any reason that IO.write() (or File.write()) is not
provided?
I have to define File.write() for each project...
It is easy to define File.write() but I hope it is provided by Ruby.

Is there a reason you can't use File's concatenation (<<) operator?

Doug
 
B

Brian Candler

I have to define File.write() for each project...

Do you mean you want:

File.write(filename, string)

to overwrite the contents of the named file with the contents of the
string?

I'd just do

File.open(filename,"w") { |f| f << string }

It's not much more typing. If you use it lots, then as you say, you can
write your own method to do it.

I find normally I'm doing something else, e.g. streaming an existing
open I/O object into a file:

IO.popen("...") do |src|
File.open(file2,"w") do |dst|
while data = src.read(16384)
dst << data
end
end
end

But it doesn't worry me that Ruby doesn't provide this natively, since
it's so easy to construct from the tools provided.

Regards,

Brian.
 
K

kwatch

Thank you for everyone who replied me.

You can as well use File.read().

I know it. My question is that File.read() is more natural than
IO.read() because filename is related with File, not IO.

Why don't you just create a library of things you regularly need and
require that?

It is one solution, but not answer for my question.

Probably because writing is more dangerous and also there are more
options to file writing than to read (beginning vs. at end, create or
not, text vs. binary).

Your points apply to IO.read() as well as IO.write().
IO.read() is provided in default regardless it doesn't support binary
mode.

Don't get me wrong. I have just question, not blame.


Do you mean you want:

File.write(filename, string)

to overwrite the contents of the named file with the contents of the
string?
Yes.


I'd just do

File.open(filename,"w") { |f| f << string }

It's not much more typing. If you use it lots, then as you say, you can
write your own method to do it.

Yes, it is not so long, but IO.read(filename) is provided
in spite that Io_Open(filename) {|f| f.read } is also short.

It is odd for me that IO.read() is provided and IO.write() is not.
This is asymmetric.
 
R

Robert Klemme

Thank you for everyone who replied me.

You're welcome.
It is one solution, but not answer for my question.

I could have put it differently: why bother when there's an easy
solution that does not require the answer to the original question?
Curiosity is good, but sometimes it distracts from getting solutions.

That may sound harsh; I guess I've become a bit grumpy because of all
the "why is X?" and "can't we make Y?" type of questions. It seems with
the growing number of users there is also a significant growth in
"change requests". Unfortunately a lot of them seem not well thought
out or are about things where there are solutions that do not need a
language / core library change.
Your points apply to IO.read() as well as IO.write().
IO.read() is provided in default regardless it doesn't support binary
mode.

Not really: reading is not dangerous because it cannot destroy the file.
There is no point in starting to read at the end of the file. And
creating the file for reading does not make sense either. So, as you
can see, read and write as such are quite asymmetric. IIRC Matz said
something similar quite a while ago; you may able to dig that up in the
archives.

Cheers

robert
 
K

kwatch

Thank you, Robert.
# sorry for my poor English.

2007/09/16 said:
Not really: reading is not dangerous because it cannot destroy the file.
There is no point in starting to read at the end of the file.

* It is possible to read N bytes from the end of file, but
IO.read() doesn't support it.
Nobody wants File.write() to have all features that File.open() can
do.
IMO, appending data at the endo of file is not so much.
Most of the cases to write data into file is replacing
content of that file.
So File.write() is useful for most of the cases and you can use
File.open(..) {...} when you want to do more complex operation.

* File.write() is not the only method that is dangerous.
For example, File.unlink() is as dangerous as File.write().
File.unlink() is already provided, you know.

And creating the file for reading does not make sense either.
So, as you can see, read and write as such are quite asymmetric.

Why can the assymmetricity of characteristics be the reason of
not-to-provide IO.write() ?
File.read() and File.write() may be assymmetric in the point of each
characteristics, but to provide both methods is symmetric in the
point of Ruby's feature.

IIRC Matz said
something similar quite a while ago; you may able to dig that up in the
archives.

Thank you for good advice. I'll search it.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top