What if you could tell Ruby to do the same thing many times with just a tiny piece of code?
Why Block syntax (do..end and curly braces) in Ruby? - Purpose & Use Cases
Imagine you want to repeat a task several times or perform an action on each item in a list by writing the same code again and again.
For example, printing each name in a list by typing a separate print statement for each one.
This manual way is slow and boring. It's easy to make mistakes like forgetting one item or typing the wrong thing.
Also, if the list changes, you have to rewrite many lines, which wastes time and causes errors.
Block syntax lets you write the repeated action just once and tell Ruby to run it for each item automatically.
You wrap the code in do..end or curly braces { }, and Ruby handles the rest.
puts 'Name: Alice' puts 'Name: Bob' puts 'Name: Carol'
['Alice', 'Bob', 'Carol'].each do |name| puts "Name: #{name}" end
You can easily repeat actions on many items without extra typing, making your code shorter, clearer, and less error-prone.
Think about sending a thank-you message to every person who attended your party. Instead of writing each message separately, you write one message and use a block to send it to everyone.
Manual repetition is slow and error-prone.
Block syntax wraps repeated code neatly for automatic execution.
Using do..end or { } makes code shorter and easier to maintain.
Practice
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]
- Using curly braces for multi-line blocks reduces readability
- Thinking Ruby doesn't support blocks
- Believing both syntaxes are identical in all cases
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]
- Missing pipes around block parameters
- Using do..end without proper line breaks
- Incorrect placement of pipes or braces
(1..3).map { |x| x * 2 }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]
- Confusing map with each (which returns original array)
- Expecting a syntax error due to block syntax
- Misunderstanding range behavior
5.times do |i| puts i }
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]
- Mixing do..end with curly braces
- Forgetting to close the block
- Misplacing block parameter pipes
[1, 2, 3, 4], which block syntax correctly filters even numbers and returns them as a new array?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]
- Omitting block parameters
- Mixing do..end without pipes for parameters
- Using incorrect block syntax causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
