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

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.

···

--
regards,
makoto kuwata

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

···

2008/9/15 kwatch <kwatch@gmail.com>:

--
use.inject do |as, often| as.you_can - without end

kwatch wrote:

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

···

--
Posted via http://www.ruby-forum.com/\.

  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.

···

--
Posted via http://www.ruby-forum.com/\.

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.

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:

  File.read(path, "rb")

For File.write(), it is more likely to be:

  File.write(path, str, "wb")

which makes me feel weird. In 1.9, we can write

  File.write(path, str, mode: "wb")

which I feel slightly better.

              matz.

···

In message "Re: Q: IO.read() and IO.write()" on Mon, 15 Sep 2008 13:17:56 +0900, kwatch <kwatch@gmail.com> writes:

Thank you for everyone who replied me.

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

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

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.

···

On 2008/9/5 Robert Klemme <shortcut...@googlemail.com> wrote:
On 2008/9/15 Brian Candler <b.cand...@pobox.com> wrote:

--
regards,
makoto kuwata

Thank you, Matz.

>* 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

For File.write(), it is more likely to be:

  File.write(path, str, "wb")

which makes me feel weird. In 1.9, we can write

  File.write(path, str, mode: "wb")

which I feel slightly better.
                                                        matz.

You mean that there is no plan to add File.write() in Ruby 1.9?

···

2008/09/17 Yukihiro Matsumoto <m...@ruby-lang.org> wrote:

--
regards,
makoto kuwata

Thank you for everyone who replied me.

You're welcome.

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.

Cheers

  robert

···

On 15.09.2008 15:14, kwatch wrote:

On 2008/9/5 Robert Klemme <shortcut...@googlemail.com> wrote:

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

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

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

···

2007/09/16, Robert Klemme <shortcut...@googlemail.com> wrote:

--
regards,
makoto kuwata