Blocks let you group code to run later or multiple times. They make your code cleaner and easier to reuse.
Block syntax (do..end and curly braces) in Ruby
Introduction
Syntax
Ruby
method_name do # code here end # or method_name { # code here }
Use do..end for multi-line blocks.
Use curly braces { } for single-line blocks.
Examples
do..end block.Ruby
3.times do puts "Hello" end
Ruby
3.times { puts "Hi" }
do..end.Ruby
numbers = [1, 2, 3] numbers.each do |n| puts n * 2 end
Ruby
numbers.each { |n| puts n * 2 }
Sample Program
This program counts from 1 to 5 using a do..end block and then prints fruit names in uppercase using a curly braces block.
Ruby
5.times do |i| puts "Count: #{i + 1}" end ["apple", "banana", "cherry"].each { |fruit| puts fruit.upcase }
Important Notes
Blocks are anonymous chunks of code you can pass to methods.
Curly braces have higher precedence than do..end, so use them carefully in complex expressions.
Blocks can take parameters inside pipes | | to use values from the method.
Summary
Blocks group code to run inside methods.
Use do..end for multi-line blocks and { } for single-line blocks.
Blocks can take parameters to work with data from the method.
Practice
1. Which block syntax is generally preferred for multi-line blocks in Ruby?
easy
Solution
Step 1: Understand block syntax options
Ruby supports two block syntaxes:do..endand curly braces{ }.Step 2: Identify usage for multi-line blocks
do..endis preferred for multi-line blocks because it improves readability.Final Answer:
do..end-> Option DQuick Check:
Multi-line blocks use do..end [OK]
Hint: Use do..end for multi-line blocks, braces for single-line [OK]
Common Mistakes:
- Using curly braces for multi-line blocks reduces readability
- Thinking Ruby doesn't support blocks
- Believing both syntaxes are identical in all cases
2. Which of the following is the correct syntax for a single-line block that prints each number in an array?
easy
Solution
Step 1: Identify correct block syntax for single-line
Single-line blocks use curly braces{ }with parameters inside pipes| |.Step 2: Check syntax correctness
[1, 2, 3].each { |n| puts n } correctly uses{ |n| puts n }. Others have syntax errors.Final Answer:
[1, 2, 3].each { |n| puts n } -> Option AQuick Check:
Single-line block with braces and pipes [OK]
Hint: Use { |var| ... } for single-line blocks [OK]
Common Mistakes:
- Missing pipes around block parameters
- Using do..end without proper line breaks
- Incorrect placement of pipes or braces
3. What is the output of the following Ruby code?
(1..3).map { |x| x * 2 }medium
Solution
Step 1: Understand the range and map method
The range (1..3) produces numbers 1, 2, 3. Themapmethod applies the block to each number.Step 2: Calculate each mapped value
Each number is multiplied by 2, resulting in 2, 4, and 6.Final Answer:
[2, 4, 6] -> Option BQuick Check:
Map doubles each element [OK]
Hint: map applies block to each element and returns new array [OK]
Common Mistakes:
- Confusing map with each (which returns original array)
- Expecting a syntax error due to block syntax
- Misunderstanding range behavior
4. Identify the error in this Ruby code:
5.times do |i| puts i }
medium
Solution
Step 1: Check block opening and closing syntax
The block starts withdobut incorrectly ends with a curly brace}.Step 2: Understand correct block closure
Blocks opened withdomust close withend, not}.Final Answer:
Incorrect use of curly brace to close a do..end block -> Option CQuick Check:
do..end blocks cannot close with braces [OK]
Hint: Match do with end, braces with braces [OK]
Common Mistakes:
- Mixing do..end with curly braces
- Forgetting to close the block
- Misplacing block parameter pipes
5. Given the array
[1, 2, 3, 4], which block syntax correctly filters even numbers and returns them as a new array?hard
Solution
Step 1: Understand the select method and block parameters
selectfilters elements for which the block returns true. The block must receive each element as a parameter.Step 2: Check syntax correctness
[1, 2, 3, 4].select { |n| n.even? } correctly uses curly braces with block parameter|n|and callsn.even?. [1, 2, 3, 4].select do |n| n.even? has a syntax error (missingend). [1, 2, 3, 4].select { n.even? } misses block parameters. [1, 2, 3, 4].select do n.even? end misses pipes around parameter.Final Answer:
[1, 2, 3, 4].select { |n| n.even? } -> Option AQuick Check:
Curly braces with pipes for single-line filter [OK]
Hint: Use { |var| condition } for concise filtering blocks [OK]
Common Mistakes:
- Omitting block parameters
- Mixing do..end without pipes for parameters
- Using incorrect block syntax causing errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
