Bird
0
0

What is the issue with this Ruby class's respond_to_missing? method?

medium📝 Debug Q6 of 15
Ruby - Metaprogramming Fundamentals

What is the issue with this Ruby class's respond_to_missing? method?

class Example
  def respond_to_missing?(method_name)
    method_name == :bar
  end

  def method_missing(method_name, *args)
    "bar called" if method_name == :bar
  end
end
AThe <code>respond_to_missing?</code> method is missing the <code>include_private</code> parameter.
BThe <code>method_missing</code> method should call <code>super</code> when method_name is not :bar.
CThe <code>respond_to_missing?</code> method should return true for all methods.
DThe class should not define <code>method_missing</code> without defining <code>respond_to?</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Check respond_to_missing? signature

    The method is missing the second parameter include_private, which is required for proper overriding.
  2. Step 2: Understand consequences

    Without the correct signature, Ruby's respond_to? may not behave correctly.
  3. Final Answer:

    The respond_to_missing? method is missing the include_private parameter. -> Option A
  4. Quick Check:

    Signature must match Ruby's expected parameters [OK]
Quick Trick: respond_to_missing? must accept include_private parameter [OK]
Common Mistakes:
  • Ignoring the include_private parameter
  • Not calling super in method_missing
  • Returning incorrect boolean values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes