Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Functional Patterns in Ruby
Identify the error in this Ruby code:
p1 = Proc.new { |x| x + 5 }
p2 = Proc.new { |y| y * 2 }
p3 = p1 >> p2.call
puts p3.call(3)
AMissing parentheses in Proc.new
BUsing p2.call instead of p2 in composition
CIncorrect argument passed to p3.call
DUsing >> operator instead of +
Step-by-Step Solution
Solution:
  1. Step 1: Understand Proc composition

    The right side of >> must be a Proc, but p2.call calls the Proc immediately and returns a value.
  2. Step 2: Identify error

    Using p2.call instead of p2 causes an error because the composition expects a Proc, not a value.
  3. Final Answer:

    Using p2.call instead of p2 in composition -> Option B
  4. Quick Check:

    Check types on both sides of >> [OK]
Quick Trick: Compose Procs, not their results [OK]
Common Mistakes:
  • Calling Proc instead of passing Proc object
  • Confusing operators for composition
  • Wrong argument to call method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes