Invoking method_missing methods in Ruby Aug 28, 2009
So here’s another quick Ruby tip. Kernel.method_missing is a way to catch all invocations to methods in whatever scope you want that aren’t defined. While discouraged from widespread use, eg since it can be hard to debug and can lead to sloppy programming, it does have legitimate / good use cases, for example ActiveRecord uses it to automatically map methods to database columns without requiring developers to explicitly write a getter/setter for each.
Now lets say you want to invoke a method on an object via reflection / introspection. Normally you would run
Object.method returns an instance of the ‘Method’ object, on which you can invoke ‘call’ with parameters to invoke that method. If the named method is not found, an exception is raised.
Unfortunately this will be the case for “methods” handled by methodmissing. To invoke these methods meta-programatically, we use another feature of the ‘Object’ class, send. The Ruby backend uses message passing to invoke methods on objects among other things, and the send method explicitly sends a new method invocation message to your object, which will be caught by methodmissing if not present.