Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Functional Patterns in Ruby
What will be the output of this Ruby code?
p1 = Proc.new { |n| n * 4 }
p2 = Proc.new { |m| m - 1 }
p3 = p1 >> p2
puts p3.call(2)
A8
B7
C6
D9
Step-by-Step Solution
Solution:
  1. Step 1: Evaluate p1.call(2)

    p1 multiplies input by 4: 2 * 4 = 8
  2. Step 2: Pass result to p2

    p2 subtracts 1: 8 - 1 = 7
  3. Final Answer:

    7 -> Option B
  4. Quick Check:

    Calculate stepwise [OK]
Quick Trick: Evaluate left Proc first, then right [OK]
Common Mistakes:
  • Adding instead of subtracting in p2
  • Reversing order of composition
  • Calling p3.call with wrong argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes