Is there a quick way to track the methods that are being called on an object? Often, when I’m working with a gem at a level just below their public interface, I run into errors that are hard to track down. Ultimately, I end up tracking the object through the source code and keeping everything in my head.
But it would be nice to be able to call something like a #log_method_calls on an object so that, say, all methods called on it get printed to stdout or something. Is there any way to accomplish this?
There are several methods to do it, depending on the situation.
If it’ possible to create a new object instead of the observed, you can easily write an observer class using method_missing.
If it’s not possible, you still may use alias_method. It’s a bit more tricky, but using Module.instance_methods you can chain every method of anything.
Something like: