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?
Try Python decorators (i.e. the stuff on http://www.cse.ucsd.edu/classes/fa05/cse130/pa4/pa4.html). They’re actually pretty neat. :)
In the words of Dr. Cham,
“Stunning.”