Explain method_missing and respond_to_missing? for dynamic dispatch.
Updated May 17, 2026
Short answer
method_missing catches unhandled invocations dynamically. respond_to_missing? ensures reflection tools like respond_to? correctly recognize these dynamic targets.
Deep explanation
When Ruby looks up a method and fails to find it in the class hierarchy, it climbs back down and invokes method_missing(method_name, *args, &block). This allows developers to catch arbitrary dynamic calls. However, overriding method_missing breaks standard introspection unless you also override respond_to_missing?(method_name, include_private). This tells Ruby's introspection system that your object does, in fact, handle that method call.
Real-world example
Historically heavily utilized in ActiveRecord for dynamic query interfaces like User.find_by_email_and_status(...), routing attribute lookups directly onto hash columns.
Common mistakes
- Forgetting to call `super` inside `method_missing` for unhandled naming schemas, which completely suppresses regular `NoMethodError` warnings and creates impossible debugging loops.
Follow-up questions
- Why is relying heavily on `method_missing` slow?
- What is a faster metaprogramming alternative to `method_missing`?