Challenge - 5 Problems
Ruby Block Syntax Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of block with do..end vs curly braces
What is the output of the following Ruby code?
Ruby
def test_block yield end result = [] test_block do result << 1 end test_block { result << 2 } puts result.inspect
Attempts:
2 left
💡 Hint
Both do..end and curly braces can be used to pass blocks to methods.
✗ Incorrect
Both blocks add elements to the same array in order: first 1, then 2.
❓ Predict Output
intermediate2:00remaining
Return value of block with do..end vs curly braces
What is the output of this Ruby code?
Ruby
def block_return result = yield puts result end block_return do 5 + 3 end block_return { 10 + 2 }
Attempts:
2 left
💡 Hint
The yield keyword returns the value of the block.
✗ Incorrect
The block returns 8 and 12 respectively, which are printed by puts.
🔧 Debug
advanced2:00remaining
Identify the syntax error in block usage
Which option contains a syntax error when defining a block in Ruby?
Attempts:
2 left
💡 Hint
Check if the block delimiters match correctly.
✗ Incorrect
Option B mixes curly braces and do..end incorrectly, causing a syntax error.
🧠 Conceptual
advanced2:00remaining
Difference in precedence between do..end and curly braces
Which statement about Ruby block syntax precedence is true?
Attempts:
2 left
💡 Hint
Think about how Ruby parses method calls with blocks.
✗ Incorrect
Curly braces bind more tightly to the method call than do..end blocks, affecting how arguments are parsed.
🚀 Application
expert3:00remaining
Predict output with nested blocks and different syntax
What is the output of this Ruby code?
Ruby
def outer yield end outer do outer { puts 'Inner block' } puts 'Outer block' end
Attempts:
2 left
💡 Hint
Blocks can be nested and execute in order of yield calls.
✗ Incorrect
The inner block prints first because it is yielded inside the outer block before printing 'Outer block'.