Bird
0
0

How can you use method_missing to create a dynamic proxy that logs all method calls and forwards them to an internal object?

hard📝 Application Q8 of 15
Ruby - Metaprogramming Fundamentals

How can you use method_missing to create a dynamic proxy that logs all method calls and forwards them to an internal object?

ADefine <code>method_missing</code> to ignore all calls silently.
BOverride all methods manually instead of using method_missing.
CUse <code>method_missing</code> to raise errors for all calls.
DDefine <code>method_missing</code> to log the call, then use <code>send</code> to forward to the internal object.
Step-by-Step Solution
Solution:
  1. Step 1: Understand dynamic proxy pattern

    A proxy intercepts calls, logs them, then forwards to the real object.
  2. Step 2: Use method_missing for catch-all

    Implement method_missing to log method name and args, then call send on internal object.
  3. Final Answer:

    Define method_missing to log the call, then use send to forward to the internal object. -> Option D
  4. Quick Check:

    method_missing + send = dynamic proxy with logging [OK]
Quick Trick: Use method_missing + send to forward calls dynamically [OK]
Common Mistakes:
  • Manually overriding all methods instead of using method_missing
  • Raising errors instead of forwarding calls
  • Ignoring calls silently losing functionality

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes