4.3. The method_missing method
The Kernel module provides an instance method called method_missing. This method is executed whenever an object receives a message that it doesn’t know how to respond to—that is, a message that doesn’t match a method anywhere in the object’s method-lookup path:
>> o = Object.new => #<Object:0x0000010141bbb0> >> o.blah NoMethodError: undefined method `blah' for #<Object:0x0000010141bbb0>
It’s easy to intercept calls to missing methods. You override method_missing, either on a singleton basis for the object you’re calling the method on, or in the object’s class or one of that class’s ancestors:
When you override
Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.