In message "[ruby-talk:00668] Way to intercept method calls?"
on 99/08/16, Clemens Hintze <c.hintze / gmx.net> writes:
|and I have defined:
|
| foo = Foo.new(12)
|
|and now I want to call:
|
| foo.blarb
|
|This call will results in a `foo.method_missing(:blarb)', right?
Right.
|Now I want to know, whether there is a possibility to catch `foo.show'
|within `Foo'? Is there some mechanism like:
|
| foo.show
|
|will results in `foo.__send__(:show)', so that I could redefine
|`Foo#__send__' and therefore capture all method calls to `foo'?
Hmm, there's no such mechanism to capture all method calls in current
Ruby, and I'm afraid it will not be available in the future, because
it should cause performance drawback.
Instead, there's a way to override all predefined methods dynamically,
which is not exactly you want, since it can't handle dynamically
defined methods, but I guess it will do most of the job.
e.g.
for m in obj.methods
eval <<END
def obj.#{m}(*args)
obj.__send__(:#{m}, *args)
end
END
end
matz.