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

Discussion in 'Ruby' started by kwatch, Sep 15, 2008.

  1. kwatch

    kwatch Guest

    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
     
    kwatch, Sep 15, 2008
    #1
    1. Advertising

  2. 2008/9/15 kwatch <>:
    > 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
     
    Robert Klemme, Sep 15, 2008
    #2
    1. Advertising

  3. kwatch

    Doug Glidden Guest

    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/.
     
    Doug Glidden, Sep 15, 2008
    #3
  4. > 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/.
     
    Brian Candler, Sep 15, 2008
    #4
  5. kwatch

    kwatch Guest

    Thank you for everyone who replied me.

    On 2008/9/5 Robert Klemme <> 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 <> 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
     
    kwatch, Sep 15, 2008
    #5
  6. On 15.09.2008 15:14, kwatch wrote:
    > Thank you for everyone who replied me.


    You're welcome.

    > On 2008/9/5 Robert Klemme <> 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
     
    Robert Klemme, Sep 15, 2008
    #6
  7. kwatch

    kwatch Guest

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

    2007/09/16, Robert Klemme <> 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
     
    kwatch, Sep 16, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page