Bird
0
0

How can you modify this Ruby class so that respond_to? correctly returns true for methods starting with process_?

hard📝 Application Q9 of 15
Ruby - Metaprogramming Fundamentals

How can you modify this Ruby class so that respond_to? correctly returns true for methods starting with process_?

class Processor
  def method_missing(method_name, *args)
    if method_name.to_s.start_with?("process_")
      "Processing #{args.join(", ")}" 
    else
      super
    end
  end
end
AAdd <code>def respond_to_missing?(method_name); true; end</code>
BOverride <code>respond_to?</code> to always return true.
CAdd <code>def respond_to_missing?(method_name, include_private = false); method_name.to_s.start_with?("process_"); end</code>
DNo modification needed; respond_to? works automatically.
Step-by-Step Solution
Solution:
  1. Step 1: Understand respond_to? behavior

    By default, respond_to? does not know about dynamic methods handled by method_missing.
  2. Step 2: Implement respond_to_missing?

    To inform respond_to? about dynamic methods, define respond_to_missing? with the same logic as method_missing uses.
  3. Step 3: Correct implementation

    Define respond_to_missing?(method_name, include_private = false) returning true if method_name starts with "process_".
  4. Final Answer:

    Add respond_to_missing? with method_name and include_private parameter checking method_name prefix. -> Option C
  5. Quick Check:

    Does respond_to_missing? match method_missing logic? Yes. [OK]
Quick Trick: Match respond_to_missing? logic with method_missing for dynamic methods [OK]
Common Mistakes:
  • Overriding respond_to? incorrectly
  • Omitting include_private parameter
  • Assuming respond_to? auto-detects method_missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes