From: Dennis Decker Jensen Date: 2001-03-06T06:34:50+09:00 Subject: [ruby-talk:12061] Ruby & AOP Hi ! I've followed the threads about aspects in ruby. It made me re-check the section in the pickaxe-book about tracing a programs execution. Just had a wild thought in the middle of all this talk about AOP and Ruby facilities... set_trace_func and set_trace_var seems efficient, but not very elegant. Also they trace every code line, which is seems like overkill regarding AOP. Then there are the callbacks: Module#method_added # Adding an instance method Kernel.singleton_method_added # Adding a singleton method Class#inherited # Subclassing a class Module#extend_object # Mixing in a module These seem not only easy and readable, but are also very elegant. I've used them my self a couple of times. So here is my main thougth: What about adding another callback named "called"? Object.called seems like the right place..(??) It could be used something like this (wild fantasy code snippet): --------------------- class Thing attr_reader :x def initialize(x, y) @x,@y = x, y end def printXAndY print "x: #{@x} y: #{@y}\n" end def Thing.called(object, method, returned_object, *args) puts "In class method: " print "#{object}.#{method}(#{args}\n" end def called(object, method, returned_object, *args) puts "In instance method: " print "#{object}.#{method}(#{args})\n" args[0] = 7 if method =~ "printXandY" end end t = Thing.new(12,24) # would generate something like: In class method: Thing.new(12, 24) In instance method: t.initialize(12, 24) t.printXandY # would generate something like: In instance method: t.printXandY() puts t.x # would generate something like: In instance method: t.x() 7 --------------------- It looks nice from a usage point of view. Pure Star Trek! Is it feasible? The idea is to pass arguments similar to the ones passed on in AspectR and be able to change the arguments in place. Oh - wait - there is going to be a problem with that... Ruby variable assignment only changes the reference and not the object itself. Sigh! I'm sorry about that. I'm afraid though that I haven't got a clue regarding the implementation or issues involved. Does anyone have knowledge to see if it could be possible somehow? Would it be worth the effort? Dennis Decker Jensen