respond_to_missing? is the callback that keeps Ruby's introspection consistent with method_missing. When an object handles an otherwise unknown message dynamically, respond_to? can consult this method before answering whether the message is supported. section 15.1.1
Making dynamic setters discoverable
A Person object can turn person.set_age(38) into an age= call through method_missing. Without respond_to_missing?, person.respond_to?(:set_age) reports false even though the call works. Defining respond_to_missing? to recognize set_ makes the query return true; person.method(:set_age) can then produce a method object. section 15.1.1
def respond_to_missing?(m, include_private = false)
/set_/.match(m) || super
end
The callback recognizes dynamically handled set_ messages and delegates unrelated queries to super. Its second argument receives respond_to?'s private-method preference. section 15.1.1
Keep these distinctions in mind:
- Define respond_to_missing? when method_missing handles a family of messages that callers may need to discover. section 15.1.1
- Let respond_to? invoke the callback; respond_to_missing? is a supplemental helper and is not intended to be called directly. section 15.1.1
