0
0
Rubyprogramming~20 mins

Block parameters in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Block Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A2
B1
Cnil
D3
Attempts:
2 left
💡 Hint
Remember that block parameters receive values passed by yield in order.
Predict Output
intermediate
2: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
A10
B1020
C200
DError
Attempts:
2 left
💡 Hint
The block parameter uses parentheses to destructure the array.
Predict Output
advanced
2: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
A15
BError
C5
D10
Attempts:
2 left
💡 Hint
Check how default values work in block parameters when fewer arguments are passed.
Predict Output
advanced
2: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
AError
B6
C3
D5
Attempts:
2 left
💡 Hint
The splat operator collects remaining arguments into an array.
Predict Output
expert
3: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
A6
B7
CError
D8
Attempts:
2 left
💡 Hint
Look carefully at how default values apply inside nested destructuring.