From: Robert Klemme Date: 2005-04-05T04:19:44+09:00 Subject: Re: Wrote "Hookable" mixin, what now? "Jan 'jast' Krueger" schrieb im Newsbeitrag news:3bdg29F6hjs0oU1@news.dfncis.de... > Hi everyone, > > I've just written a mixin that allows hooking into methods of a class. You're not the first one. :-) Others have done similar things: http://www.rubygarden.org/ruby?MethodHooks (Disclaimer: I would do it probably different nowadays.) > Since an example is worth more than a hundred words, here is one (taken > from > the documentation, slightly adapted). > > --- begin example --- > Now what do I do with it? Some people in #ruby-lang on freenode have told > me > they like the general idea, but I'd like more suggestions for improvements > (implementation, API, whatever) before I submit it to RAA (is submitting > it > a reasonable idea)? > > This is similar to a suggestion for Ruby 2.0 I've read about somewhere > that > extends the language itself, to allow something like def method:pre/post. Yep, that is in sync with my current knowledge. > I > think my approach, though not quite finished yet, is somewhat more > powerful > than that, and also slightly more powerful than AspectR. I haven't done a > full analysis on Hookable's performance yet but I think it should be okay. Could you explain in which ways it is more powerful? I see from your example that one has to inherit from a certain class and that you use super to invoke original methods. That might not work in all cases, i.e., when you cannot determine the class of instances created somewhere else. Wrapping methods with an approach like the one I have demonstrated works always because you can open a class any time. Kind regards robert