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.
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).
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?
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.
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.
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).
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.
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.
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.
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().
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.
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).
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.
In message “Re: Q: IO.read() and IO.write()”
on Mon, 15 Sep 2008 13:17:56 +0900, kwatch [email protected]
writes:
|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.
No big reason. Historically IO works on files, i.e. IO.open(path),
so that it was natural to provide these methods in IO too.
|* 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.
For File.read(), when you want to specify file mode, you can just say:
|* 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.
No big reason. Historically IO works on files, i.e. IO.open(path),
so that it was natural to provide these methods in IO too.
File.open() is different from IO.open().
IO.open(fd) takes file descriptor as argument,
while File.open(filename) takes filename.
irb(main):012:0> File.open(‘/tmp/hoge.txt’) {|f| f.read }
=> “foo”
irb(main):013:0> IO.open(‘/tmp/hoge.txt’) {|f| f.read }
TypeError: can’t convert String into Integer
from (irb):13:in initialize' from (irb):13:in open’
from (irb):13
IO.open() can’t take filename as argument, so it is odd for me
that IO.read() is defined in IO class, not File class.
|* 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.
For File.read(), when you want to specify file mode, you can just say:
File.read(path, “rb”)
Ruby’s manual says that 2nd argument of File.read() is length to read.
irb(main):016:0> File.read(‘/tmp/hoge.txt’)
=> “foo”
irb(main):017:0> File.read(‘/tmp/hoge.txt’, 2)
=> “fo”
irb(main):018:0> File.read(‘/tmp/hoge.txt’, ‘rb’) # ERROR!
TypeError: can’t convert String into Integer
from (irb):17:in `read’
from (irb):17
matz.
You mean that there is no plan to add File.write() in Ruby 1.9?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.