Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Functional Patterns in Ruby
What is the output of this Ruby code?
p1 = Proc.new { |x| x + 2 }
p2 = Proc.new { |y| y * 3 }
combined = p1 >> p2
puts combined.call(4)
A18
B14
C20
D12
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate first Proc with input 4

    p1 adds 2 to input: 4 + 2 = 6.
  2. Step 2: Pass result 6 to second Proc

    p2 multiplies input by 3: 6 * 3 = 18.
  3. Final Answer:

    18 -> Option A
  4. Quick Check:

    (4 + 2) * 3 = 18 [OK]
Quick Trick: Apply first Proc, then second on result [OK]
Common Mistakes:
  • Adding instead of multiplying in second Proc
  • Multiplying first, then adding
  • Confusing order of Procs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes