MΓ³dulo 27 Β· Metaprogramming & Rails Magic β LecciΓ³n 4 de 6 Β· ~9 min
method_missing & Ghost Methods
The last stop in method lookup
Remember the ancestor chain from module 24? When Ruby walks it and finds nothing, it doesn't give up β it sends the object one more message: method_missing(name, args). The default implementation raises NoMethodError. But override it, and you can respond to methods that don't exist* β "ghost methods." This is how find_by_email_and_status works in Rails without anyone defining it.
In an interview, say: "
method_missingis the hook Ruby calls when method lookup fails. Overriding it lets an object answer messages it has no explicit method for β the basis of ActiveRecord's dynamic finders and OpenStruct β as long as you pair it withrespond_to_missing?."