Challenge - 5 Problems
Proc Composition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of composed Procs with chaining
What is the output of this Ruby code using Proc composition?
Ruby
add_two = Proc.new { |x| x + 2 } double = Proc.new { |x| x * 2 } composed = add_two >> double puts composed.call(3)
Attempts:
2 left
💡 Hint
Remember that '>>' composes Procs so the first runs, then the second runs on the result.
✗ Incorrect
The 'add_two' Proc adds 2 to 3, resulting in 5. Then 'double' multiplies 5 by 2, resulting in 10.
❓ Predict Output
intermediate2:00remaining
Output of composed Procs with reverse chaining
What is the output of this Ruby code using Proc composition with '<<' operator?
Ruby
add_two = Proc.new { |x| x + 2 } double = Proc.new { |x| x * 2 } composed = add_two << double puts composed.call(3)
Attempts:
2 left
💡 Hint
The '<<' operator composes Procs so the second runs first, then the first runs on the result.
✗ Incorrect
The 'double' Proc runs first on 3, giving 6. Then 'add_two' adds 2 to 6, resulting in 8.
🔧 Debug
advanced2:00remaining
Identify the error in Proc composition
What error does this Ruby code raise when trying to compose Procs?
Ruby
p1 = Proc.new { |x| x + 1 } p2 = Proc.new { |x, y| x * y } composed = p1 >> p2 puts composed.call(2)
Attempts:
2 left
💡 Hint
Check the number of arguments each Proc expects and what is passed during composition.
✗ Incorrect
The first Proc returns one value, but the second Proc expects two arguments, causing an ArgumentError when called with one argument.
🧠 Conceptual
advanced2:00remaining
Understanding Proc composition behavior
Which statement best describes the behavior of the '>>' operator when composing two Procs in Ruby?
Attempts:
2 left
💡 Hint
Think about the order of execution and how the output of one Proc is used.
✗ Incorrect
The '>>' operator composes Procs so that the first runs, then its output is passed as input to the second Proc.
🚀 Application
expert3:00remaining
Compose Procs to transform and filter data
Given these Procs, which composed Proc will correctly double numbers, then keep only those greater than 10 when applied to an array?
Ruby
double = Proc.new { |x| x * 2 } filter_gt_10 = Proc.new { |arr| arr.select { |n| n > 10 } }
Attempts:
2 left
💡 Hint
Remember that 'double' works on single numbers, but 'filter_gt_10' works on arrays.
✗ Incorrect
Option A correctly maps 'double' over the array, then filters with 'filter_gt_10'. Options A and B try to compose incompatible Procs. Option A is correct Ruby code but not using Proc composition.