📖 Scenario: You are organizing a small party and have a list of guests with their ages. You want to greet each guest personally and mention if they are an adult or not.
🎯 Goal: Learn how to use block parameters in Ruby to process each item in a list and print customized messages.
📋 What You'll Learn
Create an array of guest names and ages
Use a block with parameters to access each guest's name and age
Print a greeting message for each guest mentioning if they are an adult (age 18 or older) or not
💡 Why This Matters
🌍 Real World
Using block parameters helps process lists of data like guest lists, orders, or records easily and clearly.
💼 Career
Understanding block parameters is essential for Ruby developers to write clean, readable, and efficient code when working with collections.
Progress0 / 4 steps
1
Create the guests array
Create an array called guests with these exact elements: [['Alice', 17], ['Bob', 22], ['Charlie', 15]]
Ruby
Hint
Use square brackets to create an array of arrays. Each inner array has a name and an age.
2
Set the adult age limit
Create a variable called adult_age and set it to 18
Ruby
Hint
Just assign the number 18 to the variable adult_age.
3
Use block parameters to greet guests
Use guests.each do |name, age| to loop over the guests array and inside the block, create a variable status that is 'an adult' if age >= adult_age or 'not an adult' otherwise
Ruby
Hint
Use the block parameters |name, age| to get each guest's details. Use a ternary operator to set status.
4
Print greeting messages
Inside the block, add a puts statement to print: "Hello, #{name}! You are #{status}."
Ruby
Hint
Use puts with string interpolation to print the message.
Practice
(1/5)
1.
What are block parameters in Ruby?
Choose the best description.
easy
A. Variables inside | | that receive values passed to the block.
B. Methods that define how a block works.
C. Special keywords to start a block.
D. Constants used only outside blocks.
Solution
Step 1: Understand block parameters role
Block parameters are variables placed inside | | after do or { to receive values passed into the block.
Step 2: Compare options with definition
Only Variables inside | | that receive values passed to the block. correctly describes block parameters as variables inside | | that get values passed to the block.
Final Answer:
Variables inside | | that receive values passed to the block. -> Option A
Quick Check:
Block parameters = variables inside | | [OK]
Hint: Block parameters are always inside | | after do or { [OK]
Common Mistakes:
Confusing block parameters with methods
Thinking block parameters are keywords
Assuming block parameters are constants
2.
Which of the following is the correct syntax for block parameters in Ruby?
array.each do ?
easy
A. [item] puts item
B. (item) puts item
C. |item| puts item
D. {item} puts item
Solution
Step 1: Recall block parameter syntax
Block parameters are placed inside vertical bars | | after do or {.
Step 2: Match syntax with options
Only |item| puts item uses |item| which is the correct syntax for block parameters.
Final Answer:
|item| puts item -> Option C
Quick Check:
Block parameters use | | [OK]
Hint: Block parameters always go inside | | after do or { [OK]
Common Mistakes:
Using parentheses () instead of | |
Using curly braces {} for parameters
Using square brackets [] for parameters
3.
What is the output of this Ruby code?
numbers = [1, 2, 3]
numbers.each do |n|
puts n * 2
end
medium
A. 2
4
6
B. 1
2
3
C. Error: undefined variable n
D. nil
Solution
Step 1: Understand the block parameter usage
The block parameter n takes each element from the array numbers one by one.
Step 2: Calculate output for each element
For each n, the code prints n * 2. So outputs are 2, 4, and 6 on separate lines.
Final Answer:
2
4
6 -> Option A
Quick Check:
Each number doubled = 2,4,6 [OK]
Hint: Block parameter holds each item; multiply inside block [OK]
Common Mistakes:
Confusing block parameter with array variable
Expecting original numbers printed
Thinking block parameter is undefined
4.
Find the error in this Ruby code:
fruits = ['apple', 'banana']
fruits.each do fruit|
puts fruit
end
medium
A. Block parameter should be in parentheses
B. Missing closing | after block parameter
C. No error, code runs fine
D. Missing opening | before block parameter
Solution
Step 1: Check block parameter syntax
Block parameters must be enclosed inside vertical bars | |. Here, only the closing bar is present.
Step 2: Identify missing part
The opening | before fruit is missing, causing a syntax error.
Final Answer:
Missing opening | before block parameter -> Option D
Quick Check:
Block parameters need both | | [OK]
Hint: Block parameters always need both | at start and end [OK]
Common Mistakes:
Forgetting the opening |
Using parentheses instead of | |
Assuming code runs without |
5.
Given this code, what will be the output?
pairs = [[1, 2], [3, 4], [5, 6]]
pairs.each do |a, b|
puts a + b
end
hard
A. [1, 2]
[3, 4]
[5, 6]
B. 3
7
11
C. Error: wrong number of block parameters
D. 1
3
5
Solution
Step 1: Understand multiple block parameters
The block parameters a and b receive each inner array's two elements separately.
Step 2: Calculate sum for each pair
For each pair, the code prints the sum: 1+2=3, 3+4=7, 5+6=11.
Final Answer:
3
7
11 -> Option B
Quick Check:
Sum of pairs = 3,7,11 [OK]
Hint: Use multiple block parameters to unpack arrays inside each [OK]
Common Mistakes:
Expecting array printed instead of sum
Thinking block parameters can't unpack arrays
Assuming syntax error with two parameters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.