The respond_to_missing? method helps Ruby objects say if they can handle a method call that is not explicitly defined. It works with method_missing to make objects behave more naturally.
Respond_to_missing? convention in Ruby
def respond_to_missing?(method_name, include_private = false) # return true or false end
This method takes the method name as a symbol and an optional flag for private methods.
It should return true if the object can handle the method, false otherwise.
find_.def respond_to_missing?(method_name, include_private = false) method_name.to_s.start_with?('find_') end
:foo or :bar, otherwise calls the parent method.def respond_to_missing?(method_name, include_private = false) [:foo, :bar].include?(method_name) || super end
This program creates a class that handles any method starting with say_. It uses method_missing to catch those calls and respond_to_missing? to say it can respond to them.
class DynamicResponder def method_missing(method_name, *args, &block) if method_name.to_s.start_with?('say_') "You called: #{method_name}" else super end end def respond_to_missing?(method_name, include_private = false) method_name.to_s.start_with?('say_') || super end end obj = DynamicResponder.new puts obj.say_hello puts obj.respond_to?(:say_hello) puts obj.respond_to?(:unknown_method)
Always define respond_to_missing? when you override method_missing to keep respond_to? accurate.
Call super in respond_to_missing? to keep normal behavior for other methods.
This helps tools like irb, debuggers, and libraries understand your object better.
respond_to_missing? tells Ruby if an object can handle a method not explicitly defined.
It works with method_missing to make dynamic method handling smooth and clear.
Always implement respond_to_missing? when using method_missing for better behavior and introspection.
Practice
What is the main purpose of respond_to_missing? in Ruby?
Solution
Step 1: Understand the role of respond_to_missing?
This method is used to inform Ruby whether an object can respond to a method that is not explicitly defined in the class.Step 2: Differentiate from other options
It does not define methods automatically, raise errors, or list methods; it only answers if a method can be handled dynamically.Final Answer:
To tell Ruby if an object can handle a method not explicitly defined -> Option AQuick Check:
respond_to_missing? = informs about dynamic methods [OK]
- Thinking it defines missing methods automatically
- Confusing it with method_missing itself
- Assuming it raises errors for missing methods
Which of the following is the correct way to define respond_to_missing? in a Ruby class?
class MyClass
def respond_to_missing?(method_name, include_private = false)
# ???
end
endSolution
Step 1: Check the method signature and expected return
respond_to_missing? should return true or false depending on whether the object can handle the method dynamically.Step 2: Analyze each option
return true if method_name.to_s.start_with?("find_") returns true if the method name starts with "find_", which is a common pattern for dynamic methods. return method_name == :find_user only returns true for one exact method, which is valid but less flexible. return false always returns false, which defeats the purpose. return method_missing(method_name) incorrectly calls method_missing, which is not correct here.Final Answer:
return true if method_name.to_s.start_with?("find_") -> Option BQuick Check:
respond_to_missing? returns true for dynamic method patterns [OK]
- Calling method_missing inside respond_to_missing?
- Always returning false
- Checking method_name without converting to string
What will be the output of the following Ruby code?
class Dynamic
def respond_to_missing?(method_name, _)
method_name.to_s.start_with?("say_")
end
def method_missing(method_name, *args)
if respond_to_missing?(method_name, false)
"You called #{method_name} with #{args.join(", ")}"
else
super
end
end
end
d = Dynamic.new
puts d.say_hello("world")
puts d.unknown_methodSolution
Step 1: Analyze respond_to_missing? behavior
It returns true only for methods starting with "say_".Step 2: Trace method calls
d.say_hello("world") triggers method_missing, which checks respond_to_missing? and returns true, so it returns the string. d.unknown_method triggers method_missing, respond_to_missing? returns false, so super is called, raising NoMethodError.Final Answer:
You called say_hello with world NoMethodError -> Option AQuick Check:
respond_to_missing? controls method_missing handling [OK]
- Assuming all missing methods are handled
- Ignoring super call in method_missing
- Not matching method_name pattern correctly
Identify the error in this Ruby class using respond_to_missing? and method_missing:
class Broken
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?("do_")
end
def method_missing(method_name, *args)
"Doing #{method_name}"
end
end
b = Broken.new
puts b.do_something
puts b.unknownSolution
Step 1: Understand method_missing behavior
method_missing should handle only methods that respond_to_missing? returns true for; otherwise, it should call super to raise NoMethodError.Step 2: Identify the problem in the code
Here, method_missing always returns a string, even for unknown methods, so it hides errors and breaks expected behavior.Final Answer:
method_missing should call super for unknown methods -> Option CQuick Check:
method_missing must call super if method not handled [OK]
- Not calling super in method_missing
- Returning wrong types from method_missing
- Misusing respond_to_missing? signature
You want to create a Ruby class that dynamically handles methods starting with calc_ by returning the method name reversed. Which respond_to_missing? and method_missing implementation correctly follows the convention?
Solution
Step 1: Verify respond_to_missing? logic
The correct version returns true only if the method_name.to_s starts with "calc_" and ignores the include_private parameter using _.Step 2: Verify method_missing logic
The correct version checks respond_to_missing?(method_name, false) before returning method_name.to_s.reverse, and calls super otherwise.Final Answer:
def respond_to_missing?(method_name, _) method_name.to_s.start_with?("calc_") end def method_missing(method_name, *args) if respond_to_missing?(method_name, false) method_name.to_s.reverse else super end end -> Option DQuick Check:
Use respond_to_missing? check and call super in method_missing [OK]
- Not calling super in method_missing
- Using include? instead of start_with?
- Always returning true in respond_to_missing?
Ready to prove it?
Pass the test and this pattern moves unknown
