On Mon, 28 Jun 2004 06:20:04 +0900, Sean O'Dell <sean / celsoft.com> wrote:
> 
> On Sunday 27 June 2004 13:44, Patrick May wrote:
> >
> > Is the problem that you want to add methods?  Then use Modules.
> 
> This is a good question.  In the past, I've wished I could change objects into
> a different state, sort of like taint or freeze, but with a little more
> customization.  For example, taking a File object and replacing all of its
> write methods to custom ones that log everything written.  It can be done by
> adding methods (and removing methods ones you don't want to be available
> anymore), but I thought a really super simple way to handle the situation is
> to simply replace the entire class of the object, so you control 100% what
> methods are available.  That way, if the File object got passed to some other
> method out of your control that called a method you forgot to replace or
> remove, the method wouldn't even be there.
> 
> Of course, like I said, the Ruby internals make difficult to make a stable
> feature, so the point is probably moot.  But what I found interesting was how
> easy it was to change an object's class in C, and to provide a fair amount of
> protective code by simply adding a Check_Type call to the R* macros.
> 
> >    * #become creates a duplicate lifecycle for objects, resulting in
> > lots of complication
> >    * It doesn't really add that much functionality
> 
> I think if Ruby could handle it with stability, it would simply be another
> possibility.  Some people would find it hard to use, others would find it
> very powerful.  I personally am not afraid of such things; I find myself
> thinking of endless uses for it.
> 
>         Sean O'Dell
> 
> 

This particular example you propose with files can be done with
Aspect Oriented Programming.
For that specific need there's this: http://aspectr.sourceforge.net/ 
by Avi Bryant and Robert Feldt.
It's still alpha, and has been so for about two years, so I'm not 
sure if you want to use it in the real world.
Anyway it's a nice thing to toy with.

cheers,

--- vruz