Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q5 of 15
Ruby - Metaprogramming Fundamentals
What will this Ruby code output?
class Calculator
  def add(a, b)
    a + b
  end
end

calc = Calculator.new
puts calc.send(:add, 5, 7)
A12
B57
Cadd
DNoMethodError
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the add method

    The add method returns the sum of two numbers.
  2. Step 2: Using send with arguments

    calc.send(:add, 5, 7) calls add with 5 and 7, returning 12.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    send passes arguments to method and returns result [OK]
Quick Trick: send passes arguments just like normal method call [OK]
Common Mistakes:
  • Concatenating numbers as strings
  • Expecting method name printed
  • Mistaking NoMethodError for valid call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes