Bird
0
0

Which of the following is the correct syntax to define method_missing in a Ruby class?

easy📝 Syntax Q12 of 15
Ruby - Metaprogramming Fundamentals

Which of the following is the correct syntax to define method_missing in a Ruby class?

class Example
  # Your code here
end
Adef method_missing(method_name) puts "Unknown method: #{method_name}" end
Bdef method_missing(method_name, args) puts "Unknown method: #{method_name} with args: #{args}" end
Cdef method_missing(method_name, *args) puts "Unknown method: #{method_name} with args: #{args}" end
Ddef method_missing(*args) puts "Unknown method called" end
Step-by-Step Solution
Solution:
  1. Step 1: Recall method_missing signature

    The method receives the missing method's name as a symbol and any arguments as a splat array.
  2. Step 2: Check each option's parameters

    def method_missing(method_name, *args) puts "Unknown method: #{method_name} with args: #{args}" end correctly uses method_name, *args to capture method name and all arguments.
  3. Final Answer:

    def method_missing(method_name, *args) puts "Unknown method: #{method_name} with args: #{args}" end -> Option C
  4. Quick Check:

    Correct parameters = def method_missing(method_name, *args) puts "Unknown method: #{method_name} with args: #{args}" end [OK]
Quick Trick: method_missing needs method name and splat args [OK]
Common Mistakes:
  • Forgetting the splat (*) before args
  • Using only one parameter for method_missing
  • Passing args without splat causing errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes