From: "Simon Kröger" Date: 2006-05-21T19:56:52+09:00 Subject: Re: How to intercept all function calls of a object? uncutstone wu wrote: > What I want is something like this: > > [...code...] > > Then I can get this kind of result: > > call method hello of class HelloBye > hello > return from method hello of class HelloBye > > And similar thing for 'bye' and other methods if existed in object a. Some food for thoughts: ------------------------------------------------------------ def intercept obj meth = obj.methods - %w{__send__ __id__} klass, name = class << obj; self; end, obj.class.name meth.each do |m| klass.module_eval %[ def #{m} *a,&b puts 'in #{name}##{m}' super puts 'out #{name}##{m}' end] end obj end class HelloBye def hello puts "hello" end def bye puts "byebye" end end a = HelloBye.new intercept a #add an intercptor to a a.hello a.bye ------------------------------------------------------------ in HelloBye#hello hello out HelloBye#hello in HelloBye#bye byebye out HelloBye#bye method missing would also be an option of course, but wrapping an object into another one has several drawbacks (e.g. dup and friends don't work as expected anymore) cheers Simon