Using a Proxy class for debugging in Ruby
caboo.se: Using a Proxy class for debugging
The basic idea is, the Proxy object owns some other kind of object, passes through all method calls to it, and optionally does auditing/profiling as it does so. Good for debugging, profiling, watching where data goes, etc.
The next logical step is to replace the class object with a proxy to it.
>> Article = Proxy.new(Article)
(irb):1: warning: already initialized constant Article
It should be trivial to add some code to Proxy so that when it’s used in this way, all its instance objects will themselves be proxied. This seems to me much more elegant than monkeypatching the class’s initialize, and it can be enabled/disabled with only the line above.
Is this even remotely possible in anything but Ruby?
March 6th, 2006 at 10:00 pm
Try Python decorators (i.e. the stuff on http://www.cse.ucsd.edu/classes/fa05/cse130/pa4/pa4.html). They’re actually pretty neat. :)
March 6th, 2006 at 10:01 pm
In the words of Dr. Cham,
“Stunning.”