matz / zetabits.com (Yukihiro Matsumoto) writes:

> I don't think it's good idea to change instance_eval.  How about this
> yet another trick (using yet undocumented unbound method).

Unfortunately, sing instance methods doesn't quite work for me.

What I'm truing to do is implement something similar to Lisp's advice
system, where you can hook a function, wrapping it in some additional
code that executes before and after the original function. What I'd
like to do is something like


   class String

     hook :upcase do |_upcase, str|
       res = _upcase(str)
       res.tr '_', '~'
     end

   end

The first parameter to the block is a proc object that wraps the
original routine, and the remaining parameters are those supplied by
the caller.

I've got this working, except I can't access instance variables during 
the block's execution. The problem comes down to invoking the block
passed to 'hook' using the then-caller's context, passing in a set of
arguments at that time.

Does anyone have any ideas?


Dave