Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Q: IO.read() and IO.write()

Reply
Thread Tools

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

 
 
kwatch
Guest
Posts: n/a
 
      09-15-2008
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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-15-2008
2008/9/15 kwatch <(E-Mail Removed)>:
> 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

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

 
Reply With Quote
 
 
 
 
Doug Glidden
Guest
Posts: n/a
 
      09-15-2008
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/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      09-15-2008
> 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/.

 
Reply With Quote
 
kwatch
Guest
Posts: n/a
 
      09-15-2008
Thank you for everyone who replied me.

On 2008/9/5 Robert Klemme <(E-Mail Removed)> wrote:
>
> > 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.


On 2008/9/15 Brian Candler <(E-Mail Removed)> wrote:
> > 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.

--
regards,
makoto kuwata

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-15-2008
On 15.09.2008 15:14, kwatch wrote:
> Thank you for everyone who replied me.


You're welcome.

> On 2008/9/5 Robert Klemme <(E-Mail Removed)> wrote:


>> 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
 
Reply With Quote
 
kwatch
Guest
Posts: n/a
 
      09-16-2008
Thank you, Robert.
# sorry for my poor English.

2007/09/16, Robert Klemme <(E-Mail Removed)> wrote:
> > 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.

--
regards,
makoto kuwata

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments