Block parameters let you give names to values passed into a block. This helps you use those values inside the block easily.
Block parameters in Ruby
collection.each do |parameter1, parameter2| # code using parameter1 and parameter2 end
Block parameters go between vertical bars | | right after do or {.
You can have one or more parameters separated by commas.
num to double each number.numbers = [1, 2, 3] numbers.each do |num| puts num * 2 end
number and word to access each pair's elements.pairs = [[1, 'one'], [2, 'two']] pairs.each do |number, word| puts "#{number} means #{word}" end
This program prints a sentence for each fruit using a block parameter named fruit.
fruits = ['apple', 'banana', 'cherry'] fruits.each do |fruit| puts "I like #{fruit}s" end
If you forget block parameters, you can't access the values inside the block.
You can use either do |params| ... end or curly braces { |params| ... } for blocks.
Block parameters name the values passed into a block.
They go inside | | after do or {.
Use them to work with each item or value inside the block.
Practice
What are block parameters in Ruby?
Choose the best description.
Solution
Step 1: Understand block parameters role
Block parameters are variables placed inside| |afterdoor{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 AQuick Check:
Block parameters = variables inside | | [OK]
- Confusing block parameters with methods
- Thinking block parameters are keywords
- Assuming block parameters are constants
Which of the following is the correct syntax for block parameters in Ruby?
array.each do ?
Solution
Step 1: Recall block parameter syntax
Block parameters are placed inside vertical bars| |afterdoor{.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 CQuick Check:
Block parameters use | | [OK]
- Using parentheses () instead of | |
- Using curly braces {} for parameters
- Using square brackets [] for parameters
What is the output of this Ruby code?
numbers = [1, 2, 3] numbers.each do |n| puts n * 2 end
Solution
Step 1: Understand the block parameter usage
The block parameterntakes each element from the arraynumbersone by one.Step 2: Calculate output for each element
For eachn, the code printsn * 2. So outputs are 2, 4, and 6 on separate lines.Final Answer:
2 4 6 -> Option AQuick Check:
Each number doubled = 2,4,6 [OK]
- Confusing block parameter with array variable
- Expecting original numbers printed
- Thinking block parameter is undefined
Find the error in this Ruby code:
fruits = ['apple', 'banana'] fruits.each do fruit| puts fruit end
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|beforefruitis missing, causing a syntax error.Final Answer:
Missing opening | before block parameter -> Option DQuick Check:
Block parameters need both | | [OK]
- Forgetting the opening |
- Using parentheses instead of | |
- Assuming code runs without |
Given this code, what will be the output?
pairs = [[1, 2], [3, 4], [5, 6]] pairs.each do |a, b| puts a + b end
Solution
Step 1: Understand multiple block parameters
The block parametersaandbreceive 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 BQuick Check:
Sum of pairs = 3,7,11 [OK]
- Expecting array printed instead of sum
- Thinking block parameters can't unpack arrays
- Assuming syntax error with two parameters
Ready to prove it?
Pass the test and this pattern moves unknown
