Challenge - 5 Problems
Ruby Block Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of block parameters with multiple variables
What is the output of this Ruby code?
Ruby
def test yield(1, 2) end result = [] test do |a, b| result << a + b end puts result.first
Attempts:
2 left
💡 Hint
Remember that block parameters receive values passed by yield in order.
✗ Incorrect
The method yields two values, 1 and 2, which are assigned to a and b respectively. Adding them gives 3.
❓ Predict Output
intermediate2:00remaining
Block parameters with array destructuring
What will this Ruby code print?
Ruby
def example yield([10, 20]) end example do |(x, y)| puts x * y end
Attempts:
2 left
💡 Hint
The block parameter uses parentheses to destructure the array.
✗ Incorrect
The array [10, 20] is destructured into x=10 and y=20, so x * y is 200.
❓ Predict Output
advanced2:00remaining
Block parameters with default values
What is the output of this Ruby code?
Ruby
def call_block yield(5) end call_block do |a, b = 10| puts a + b end
Attempts:
2 left
💡 Hint
Check how default values work in block parameters when fewer arguments are passed.
✗ Incorrect
Only one argument (5) is passed, so a=5 and b uses default 10. Sum is 15.
❓ Predict Output
advanced2:00remaining
Block parameters with splat operator
What will this Ruby code output?
Ruby
def run_block yield(1, 2, 3) end run_block do |a, *rest| puts rest.sum + a end
Attempts:
2 left
💡 Hint
The splat operator collects remaining arguments into an array.
✗ Incorrect
a=1, rest=[2,3], sum of rest is 5, plus a is 6.
❓ Predict Output
expert3:00remaining
Block parameters with nested destructuring and default values
What is the output of this Ruby code?
Ruby
def complex yield([1, 2], 3) end complex do |(a, b = 5), c| puts a + b + c end
Attempts:
2 left
💡 Hint
Look carefully at how default values apply inside nested destructuring.
✗ Incorrect
The first argument is [1,2], so a=1, b=2 (default 5 ignored because array provides value), c=3. Sum is 1+2+3=6.