Bird
Raised Fist0
Rubyprogramming~10 mins

Block parameters in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Block parameters
Start method call with block
↓
Yield to block with parameters
↓
Block receives parameters
↓
Block executes with parameters
↓
Return to method after block
↓
Method continues or ends
The method calls a block and passes values to it as parameters. The block receives these parameters and runs its code using them.
Execution Sample
Ruby
def greet
  yield("Alice", 30)
end

greet do |name, age|
  puts "Name: #{name}, Age: #{age}"
end
This code calls a method that yields two values to a block, which receives them as parameters and prints them.
Execution Table
StepActionBlock ParametersBlock ExecutionOutput
1Call greet method---
2Inside greet, yield with ("Alice", 30)name = "Alice", age = 30puts "Name: Alice, Age: 30"Name: Alice, Age: 30
3Return from block to greet---
4greet method ends---
💡 Method ends after block execution completes
Variable Tracker
VariableStartAfter yield
name-"Alice"
age-30
Key Moments - 2 Insights
Why do we write |name, age| inside the block?
The block parameters |name, age| receive the values passed by yield. See execution_table step 2 where yield sends "Alice" and 30, which the block captures as name and age.
What happens if the block parameters don't match the number of yielded values?
Ruby assigns values in order; extra yielded values are ignored, missing ones become nil. This is shown in execution_table step 2 where parameters match exactly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2, what value does the block parameter 'age' have?
A30
B"Alice"
Cnil
D0
💡 Hint
Check the 'Block Parameters' column in step 2 of execution_table
At which step does the block print the output?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Block Execution' and 'Output' columns in execution_table
If we change yield("Bob", 25) in the method, what will the block parameter 'name' be at step 2?
A"Alice"
B"Bob"
C25
Dnil
💡 Hint
The block parameters get values from yield as shown in variable_tracker and execution_table step 2
Concept Snapshot
Block parameters receive values passed by yield inside a method.
Syntax: method calls yield with values, block declares parameters with |param1, param2|.
Block runs using these parameters.
If parameters mismatch, Ruby assigns in order or nil.
Blocks allow flexible code reuse with passed-in data.
Full Transcript
This example shows how Ruby methods can pass values to blocks using block parameters. The method greet calls yield with two values: "Alice" and 30. The block receives these values as parameters named name and age. Inside the block, it prints the name and age. The execution table traces each step: calling the method, yielding values, block execution with parameters, and method end. Variable tracker shows how name and age get assigned after yield. Key moments clarify why block parameters are needed and what happens if they don't match yielded values. The quiz tests understanding of parameter values and execution steps. This helps beginners see how blocks receive and use parameters step-by-step.

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

  1. Step 1: Understand block parameters role

    Block parameters are variables placed inside | | after do or { to receive values passed into the block.
  2. 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.
  3. Final Answer:

    Variables inside | | that receive values passed to the block. -> Option A
  4. 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

  1. Step 1: Recall block parameter syntax

    Block parameters are placed inside vertical bars | | after do or {.
  2. Step 2: Match syntax with options

    Only |item| puts item uses |item| which is the correct syntax for block parameters.
  3. Final Answer:

    |item| puts item -> Option C
  4. 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

  1. Step 1: Understand the block parameter usage

    The block parameter n takes each element from the array numbers one by one.
  2. 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.
  3. Final Answer:

    2 4 6 -> Option A
  4. 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

  1. Step 1: Check block parameter syntax

    Block parameters must be enclosed inside vertical bars | |. Here, only the closing bar is present.
  2. Step 2: Identify missing part

    The opening | before fruit is missing, causing a syntax error.
  3. Final Answer:

    Missing opening | before block parameter -> Option D
  4. 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

  1. Step 1: Understand multiple block parameters

    The block parameters a and b receive each inner array's two elements separately.
  2. Step 2: Calculate sum for each pair

    For each pair, the code prints the sum: 1+2=3, 3+4=7, 5+6=11.
  3. Final Answer:

    3 7 11 -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard