Recall & Review
beginner Click to reveal answer
What is a block in Ruby?
A block is a chunk of code enclosed between
do..end or curly braces { } that can be passed to methods and executed.beginner Click to reveal answer
When should you use
do..end versus curly braces { } for blocks?do..end is usually used for multi-line blocks, while curly braces { } are preferred for single-line blocks.beginner Click to reveal answer
How do you pass a block to a method in Ruby?
You write the method call followed by a block using either
do..end or { }. The method can then execute the block using yield.beginner Click to reveal answer
Example: What does this code print?
3.times do puts "Hello" end
It prints "Hello" three times, each on a new line.
intermediate Click to reveal answer
Can you use both
do..end and curly braces { } interchangeably?Yes, both define blocks, but style and readability guide when to use each. Curly braces for short blocks,
do..end for longer ones.Which block syntax is preferred for single-line blocks in Ruby?
✗ Incorrect
Curly braces {} are preferred for single-line blocks in Ruby.
What keyword inside a method executes the passed block?
✗ Incorrect
The keyword
yield runs the block passed to the method.Which of these is a valid way to write a block in Ruby?
✗ Incorrect
Blocks are written with do..end (A) or curly braces {} (C).
What will this code output?
5.times { print "*" }
5.times { print "*" }
✗ Incorrect
It prints five stars in a row without spaces.
Which block syntax is usually used for multi-line blocks?
✗ Incorrect
do..end is preferred for multi-line blocks in Ruby.Explain the difference between
do..end and curly braces { } when writing blocks in Ruby.Think about block length and style.
You got /4 concepts.
Describe how a Ruby method can use a block passed to it.
Focus on method and block interaction.
You got /4 concepts.
