Bird
0
0

Identify the error in this Ruby code that tries to compose two Procs:

medium📝 Debug Q14 of 15
Ruby - Functional Patterns in Ruby
Identify the error in this Ruby code that tries to compose two Procs:
p1 = Proc.new { |x| x * 2 }
p2 = Proc.new { |y| y + 5 }
combined = p1 >> p2.call
puts combined.call(3)
ASyntax error due to missing parentheses
BUsing wrong operator for composition
CMissing block parameters in p1
DCalling p2 immediately instead of composing
Step-by-Step Solution
Solution:
  1. Step 1: Check how p2 is used in composition

    The code uses p2.call which calls p2 immediately, returning a value, not a Proc.
  2. Step 2: Understand correct composition usage

    To compose, use p1 >> p2 without calling p2. Calling p2 breaks composition.
  3. Final Answer:

    Calling p2 immediately instead of composing -> Option D
  4. Quick Check:

    Compose Procs without calling them [OK]
Quick Trick: Do not call Procs when composing, just use >> [OK]
Common Mistakes:
  • Calling Proc instead of composing
  • Using wrong operator like <<
  • Forgetting block parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes