Bird
Raised Fist0
Rubyprogramming~10 mins

Block syntax (do..end and curly braces) in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Block syntax (do..end and curly braces)
Start
↓
Call method with block
↓
Ruby reads block syntax
↓
If do..end
↓
Execute block lines
↓
If curly braces
↓
Execute block lines
<--- Back to method↓
Method finishes
↓
End
↩<--- Back to method
Ruby methods can take blocks written with do..end or curly braces. Ruby reads the block, executes its lines, then returns to the method.
Execution Sample
Ruby
3.times do
  puts "Hi"
end
This code runs the block 3 times, printing "Hi" each time.
Execution Table
StepActionBlock SyntaxOutput
1Call 3.times with blockdo..end
2Execute block iteration 1do..endHi
3Execute block iteration 2do..endHi
4Execute block iteration 3do..endHi
5Finish methoddo..end
💡 All 3 iterations done, method ends
Variable Tracker
VariableStartAfter 1After 2After 3Final
Key Moments - 2 Insights
Why can we use both do..end and curly braces for blocks?
Both are block delimiters in Ruby. do..end is often used for multi-line blocks, curly braces for single-line, but both work the same as shown in the execution_table.
Does the block run immediately when passed?
No, the method (like times) controls when the block runs. The execution_table shows the method calls the block 3 times.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at Step 3?
A"Hi"
B"Hello"
CNothing
DError
💡 Hint
Check the Output column at Step 3 in the execution_table
At which step does the method finish running the block?
AStep 2
BStep 5
CStep 4
DStep 1
💡 Hint
Look at the Action column and Output column in the execution_table
If we replace do..end with curly braces, what changes in the output?
ACode breaks with error
BOutput changes to "Hello"
COutput stays the same
DBlock runs only once
💡 Hint
Refer to the concept_flow and key_moments about block syntax equivalence
Concept Snapshot
Ruby blocks can be written with do..end or curly braces.
Both delimit a block of code passed to methods.
Blocks run when the method calls them.
do..end is preferred for multi-line blocks.
Curly braces often used for single-line blocks.
Output depends on block code executed inside method.
Full Transcript
This lesson shows how Ruby uses blocks with two syntaxes: do..end and curly braces. When a method like times is called with a block, Ruby reads the block code inside the delimiters. The method controls how many times the block runs. For example, 3.times do puts "Hi" end runs the block 3 times, printing "Hi" each time. Both do..end and curly braces work the same way, with do..end often used for multi-line blocks and curly braces for single-line blocks. The execution table traces each step: calling the method, running the block 3 times, and finishing. This visual helps beginners see how blocks execute step-by-step.

Practice

(1/5)
1. Which block syntax is generally preferred for multi-line blocks in Ruby?
easy
A. Both are equally preferred for multi-line blocks
B. { }
C. Neither; Ruby does not support blocks
D. do..end

Solution

  1. Step 1: Understand block syntax options

    Ruby supports two block syntaxes: do..end and curly braces { }.
  2. Step 2: Identify usage for multi-line blocks

    do..end is preferred for multi-line blocks because it improves readability.
  3. Final Answer:

    do..end -> Option D
  4. Quick 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
A. [1, 2, 3].each { |n| puts n }
B. [1, 2, 3].each { n puts | }
C. [1, 2, 3].each (|n| puts n )
D. [1, 2, 3].each do |n| puts n

Solution

  1. Step 1: Identify correct block syntax for single-line

    Single-line blocks use curly braces { } with parameters inside pipes | |.
  2. Step 2: Check syntax correctness

    [1, 2, 3].each { |n| puts n } correctly uses { |n| puts n }. Others have syntax errors.
  3. Final Answer:

    [1, 2, 3].each { |n| puts n } -> Option A
  4. Quick 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
A. [1, 2, 3]
B. [2, 4, 6]
C. [3, 6, 9]
D. SyntaxError

Solution

  1. Step 1: Understand the range and map method

    The range (1..3) produces numbers 1, 2, 3. The map method applies the block to each number.
  2. Step 2: Calculate each mapped value

    Each number is multiplied by 2, resulting in 2, 4, and 6.
  3. Final Answer:

    [2, 4, 6] -> Option B
  4. Quick 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
A. Missing closing end for the block
B. Missing block parameter pipes | |
C. Incorrect use of curly brace to close a do..end block
D. No error, code runs fine

Solution

  1. Step 1: Check block opening and closing syntax

    The block starts with do but incorrectly ends with a curly brace }.
  2. Step 2: Understand correct block closure

    Blocks opened with do must close with end, not }.
  3. Final Answer:

    Incorrect use of curly brace to close a do..end block -> Option C
  4. Quick 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
A. [1, 2, 3, 4].select { |n| n.even? }
B. [1, 2, 3, 4].select do |n| n.even?
C. [1, 2, 3, 4].select { n.even? }
D. [1, 2, 3, 4].select do n.even? end

Solution

  1. Step 1: Understand the select method and block parameters

    select filters elements for which the block returns true. The block must receive each element as a parameter.
  2. Step 2: Check syntax correctness

    [1, 2, 3, 4].select { |n| n.even? } correctly uses curly braces with block parameter |n| and calls n.even?. [1, 2, 3, 4].select do |n| n.even? has a syntax error (missing end). [1, 2, 3, 4].select { n.even? } misses block parameters. [1, 2, 3, 4].select do n.even? end misses pipes around parameter.
  3. Final Answer:

    [1, 2, 3, 4].select { |n| n.even? } -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard