ruby picture

RCR 265: Readable/Writeable modules IO, StringIO etc.

Submitted by mneumann (Mon Jul 19 10:07:52 UTC 2004)

Abstract

Introduce two new modules Readable and Writeable and include them into IO, StringIO and similar modules that have the same read/write method interface.

Problem

Extending classes IO and StringIO (and others) with functionality that depends on the "read" and/or "write" interface is not easy as it is for "each" with module Enumerable.

Adding modules Readable and Writeable and include them into classes that supply methods read and write makes this a lot easier.

One example is the readbytes.rb library, which implements method readbytes(n), but only for IO objects, whereas the implementation would work without modifications as well for class StringIO.

Proposal

see Abstract.

Analysis

.

Implementation

see [ruby-core: 3212]

ruby picture
Comments Current voting

I'm definitely in favour, but would prefer the method name #readn. It's shorter and actually more intuitive (to me) than #readbytes. -- Gavin Sinclair


I restate the thread from : Imho having Readable and Writable mixins (aka Stream interfaces/facets) would be better, cause inheriting from IO is nearly impossible (as StringIO shows) and adding readbytes to IO and StringIO would just postpone the problem: just looking at the bundled libraries, what about OpenSSL::SSL::SSLSocket and Zlib::GzipReader ? -- gabriele renzi


How about adding the two modules Readable and Writeable (Writable?) in io.c, and include both of them into class IO and StringIO. "readbytes.rb" could issues an obsolete warning... and we introduce two new files "readable.rb" and "writable.rb", which fill the two modules with content? Of course we should include the modules into OpenSSL::SSL:SSLSocket and Zlib::GzipReader, too. Agreed? -- mneumann

Looking at the ruby-openssl sources, class SSLSocket seem to not to have a read or write method. Or do you refer to module Buffering in file lib/openssl/buffering.rb ?

-- mneumann


I'd like to have the two modules in the core, and let readbytes.rb add behaviour to the module IO::Writable, that would leave the behaviour in an external library like now, but still allow consistent behaviour for every Writable object.
I think SSLSocket just responds to #sysread and #syswrite, but I don't know much of the internals of OpenSSL.
Anyway, I think that you may like to provide a spec/sample implementation (fill "Analisys" ;) of the modules for better acceptance by matz.

-- gabriele renzi

for an implementation see the patch:

except, that the two modules are not in the IO namespace (but that's easy to fix).


Strongly opposed 0
Opposed 0
Neutral 0
In favor 5
Strongly advocate 3
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 .