Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Blocks, Procs, and Lambdas
What will be the output of this Ruby code?
def example
  p = Proc.new { return 5 }
  p.call
  return 10
end
puts example
Anil
B10
C5
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand Proc return behavior

    The return inside the Proc causes the entire method to return immediately with 5.
  2. Step 2: Method does not reach final return

    The return 10 after p.call is never executed because the Proc's return exits the method.
  3. Final Answer:

    5 -> Option C
  4. Quick Check:

    Proc return exits method = A [OK]
Quick Trick: Proc return exits method immediately [OK]
Common Mistakes:
  • Expecting method to return 10
  • Thinking Proc return exits only block
  • Confusing Proc and lambda return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes