On Mon, 22 Oct 2001, Derek Peschel wrote:
> And that leads up to my question.  I can imagine a "FIO-DEC IO" class that's
> just like the ordinary IO except that it also translates.  Is there a simple
> way to actually express that in Ruby?  Hopefully the new translation code
> wouldn't have to worry about the implementation of the existing code; there
> would be a small number of new methods to write; and only the class names
> would have to be changed in the callers.

what i see:

1. If you want to have IO methods filtered, a pattern to use is: define
each relevant method in a subclass FIODEC_File<File, such that every
reading method starts with a supercall and then transforms its result, or
in the case of second-order methods, the reading method is a supercall
with a block doing the filtering before yielding.

2. All the conversion process can be done by a single method called
FIDODEC_File#fiodec_to_ascii, and so the other methods would just be there
to apply *that* method as a filter.

3. If you really need to read from non-Files or non-regular-Files such as
Sockets and different subclasses of IO, then you can turn FIODEC_File into
a module FIODEC_IO; then, classes may inherit from that module, and
objects too may inherit from that module, which looks like
File.new("blahblah.old").extend(FIODEC_IO)

4. You may split that module into two modules, a generic filtering
mechanism and a filter function like #fiodec_to_ascii.

5. M?taRuby 0.7 (http://hostname.2y.net/~matju/) contains more flexible
(inheritable/overridable) versions of classes like Array, but I never got
around to coding the IO/File/Socket ones. If it happened, you wouldn't
have to write the generic part i talk about in (4), just a single method.

(i didn't talk about writing back with ascii-to-fiodec, but the ideas are
the same)

________________________________________________________________
Mathieu Bouchard                   http://hostname.2y.net/~matju