ruby picture

RCR 333: An IO.write to match IO.read

Submitted by cyent (Tue Apr 18 21:45:01 UTC 2006)

Abstract

IO.read( file_name) is one of my favourite shortcuts in Ruby.

Unfortunately the inverse is not part of the standard library.

Problem

The problem? Well two..

Proposal

Make this part of the standard API, maybe implemented in io.c

class IO

  def IO.write( file_name, string, offset = 0)
    IO.open( file_name, 'w') do |outf|
      outf.seek( offset)
      outf.write( string)
    end
  end
end

Analysis

Basically IO.read is part of the language so the converse should also be.

Implementation

Above is a simple Ruby implementation, probably a few lines of .c mostly copy&pasted from the rb_io_s_read function in io.c
ruby picture
Comments Current voting
IO.write is not sufficient, since IO is a superclass of Socket etc. as well. File.write might be, but I'm not sure how much useful the write-at-once method is.

matz.


Thanks for your reply.

Since the entry level PC these days has about 512Mb of memory, it can be used for writing any file upto about a couple of hundred megabytes.

ie. Most.

It is also a convenience thing for nifty oneliners.

This RCR is for the smallest change that would be useful.

A much larger change (and hence potentially much more useful) would be...

Place read/write it on the URI class to lift and shift files in a network transparent manner.

Oneliner ftp/http/... client.

  URI.write( "local.tgz", 
    URI.read( 
      "ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.4.tar.gz"))

In which case it would make sense to also define a copy method.

  URI.copy( "ftp://ftp.ruby-lang.org/pub/ruby/ruby-1.8.4.tar.gz",
            "local.tgz")

Where the base URI would be your current work directory for ordinary scripts or as appropriate for CGI scripts.

I'm not sure URI is the appropriate class for that, perhaps a new subclass, or perhaps a mixin, I don't know.


The problem with something like IO::read and IO::write is that it's not really cross-platform safe because it would simply assume something like:

  open(filename) { |ff| ff.read }

On Windows, that will *break* any binary file that is being read, and there's no way to force IO::read into binary mode.

IMO, this needs to be fixed first before considering something like this; otherwise, if you're wanting anything cross-platform, remember to use #open(filename, "wb") or #open(filename, "rb").


(The above posted by Austin Ziegler.)


That's OK. Make two more.

IO.read_binary IO.write_binary

JohnCarter


Actually Austin, just looking at the io.c code what you are asking is in fact a seperate RCR. You should create one if you want that.


Strongly opposed 0
Opposed 0
Neutral 1
In favor 3
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .