Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Metaprogramming Fundamentals

What will be the output of this Ruby code?

class CatchAll
  def method_missing(name, *args)
    "Called #{name} with #{args.join(', ')}"
  end
end

obj = CatchAll.new
puts obj.hello('world')
Ahello
BCalled hello with world
CNoMethodError
DCalled method_missing with hello
Step-by-Step Solution
Solution:
  1. Step 1: Understand method_missing behavior

    The method_missing catches the call to hello which is undefined and returns a string with method name and arguments.
  2. Step 2: Evaluate the output

    Calling obj.hello('world') triggers method_missing with name :hello and args ['world'], so output is "Called hello with world".
  3. Final Answer:

    Called hello with world -> Option B
  4. Quick Check:

    method_missing output = Called hello with world [OK]
Quick Trick: method_missing returns string with method name and args [OK]
Common Mistakes:
  • Expecting NoMethodError instead of method_missing output
  • Confusing method name symbol with string
  • Ignoring args passed to method_missing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes