Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Blocks, Procs, and Lambdas
What is the output of the following Ruby code?
class Calculator
  def add(x, y)
    x + y
  end
end
calc = Calculator.new
adder = calc.method(:add)
puts adder.call(5, 7)
A5 7
B12
Cadd
DNoMethodError
Step-by-Step Solution
Solution:
  1. Step 1: Understand method object creation

    The method(:add) returns a Method object for add on calc.
  2. Step 2: Calling the Method object

    Calling adder.call(5, 7) executes add(5, 7), which returns 12.
  3. Final Answer:

    12 -> Option B
  4. Quick Check:

    Method object call returns sum 12 [OK]
Quick Trick: Use call on Method object to run method with arguments [OK]
Common Mistakes:
  • Expecting method name or arguments printed
  • Confusing output with error
  • Not using call to invoke method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes