From: "T. Onoma" Date: 2003-11-27T18:08:28+09:00 Subject: Re: Method wrapping On Thursday 27 November 2003 09:13 am, Thomas Fini Hansen wrote: > On Thu, Nov 27, 2003 at 04:57:02PM +0900, T. Onoma wrote: > > On Thursday 27 November 2003 08:22 am, Gavin Sinclair wrote: > > > If you are redefining someone else's method in production, on your own > > > head be it. You should be keeping to the same interface of the > > > original, or else other code is likely to break. > > > > Exactly, and to do this requires understanding the interface. And more > > times than not, if you are REDEFINING a method in this manner, your doing > > it in totality. Juggling the hooks from before is not condusive. You'll > > end up remove_method'ing them all anyway. > > OK, I'll add my take from the perspective of someone that's not played > with Ruby that long. > > My first reaction to all this wrapper talk was 'WTF?', because it > seemed very odd to me. But I think I've gotten the idea now. It is rather odd. Aspect Oriented Programming is still rather new and I thnk has a lot of utepped potential yet. > As I see it, wrapping is for those 'I want this too' situations, you > don't really want to change the original method, but you need a bit of > extra code. Debugging statements and logging of use springs to > mind. The important part being that the extra code doesn't muck with > the interface. Let me give you an example of what you can do with AOP. I wrote a Ruby script that took an object and wrapped every method (using an eval hack) so that all changes to its instance varables were passed over to a whole different script which displayed all the info in a nice neat way. I got 100% seperation of concern between my generic object and the inteface code to watch it in action. And this in not only useful for debugging, and what not. Tie that info to a GUI toolkit script and you can farily easily create GUI front ends for your program, without touching the original code --simple automated interaction between object and user. > If you really want to change the how the method works, you do as you > do now, redefining the method itself, possibly renaming the old. Doing a wrap is little more than calling another method: def this print "A" end def that print "(" this print ")" end I have wrapped this with that. The difference with AOP wrapping is that it gives you a GENERIC means of accomplishing the same thing. So that all other methods calling on this do not have to be change to call on that. It isn't a big deal in a single class b/c I can just change the insides of this, but cutting across seperate classes, libraries, programs, etc. That isn't doable. > I might be wrong though. Nope not wrong, there's just even more to it. -t0