0
0
Rubyprogramming~10 mins

Yield to execute blocks in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Yield to execute blocks
Define method with yield
Call method
Inside method: execute code before yield
Yield: pause method, run block passed
Block runs: executes block code
Return to method after yield
Execute code after yield
Method ends
The method runs code, then pauses at yield to run the passed block, then continues after the block finishes.
Execution Sample
Ruby
def greet
  puts "Hello"
  yield
  puts "Goodbye"
end

greet { puts "How are you?" }
This code defines a method that prints 'Hello', then runs a block, then prints 'Goodbye'.
Execution Table
StepActionOutputNotes
1Call greet methodMethod starts execution
2puts "Hello"HelloPrints greeting
3yield to blockPauses method, runs block
4Block runs: puts "How are you?"How are you?Block code executes
5Return to method after yieldMethod resumes
6puts "Goodbye"GoodbyePrints farewell
7Method endsExecution complete
💡 Method finishes after printing 'Goodbye'
Variable Tracker
VariableStartAfter Step 2After Step 4Final
No variables----
Key Moments - 2 Insights
Why does the block code run between the two puts statements?
Because the method uses yield at step 3 to pause and run the block, then continues after the block finishes (see execution_table rows 3-5).
What happens if no block is given when calling the method?
The method will raise an error at yield because it expects a block to run. You can check with block_given? to avoid this.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 4?
A"How are you?"
B"Goodbye"
C"Hello"
DNothing
💡 Hint
Check the Output column at step 4 in the execution_table.
At which step does the method resume after running the block?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the Notes column for step 5 in the execution_table.
If we remove the yield line, what changes in the output?
AOnly "Hello" is printed
B"Hello" and "Goodbye" are printed, block is not run
COnly the block output is printed
DNothing is printed
💡 Hint
Yield is the only place where the block runs, so without it the block code is skipped.
Concept Snapshot
def method
  code before yield
  yield  # runs block passed to method
  code after yield
end

Calling method with a block runs the block at yield.
Without yield, block is not executed.
Full Transcript
This example shows how Ruby's yield keyword lets a method run a block of code passed to it. The method greet prints 'Hello', then yield pauses the method and runs the block which prints 'How are you?'. After the block finishes, the method resumes and prints 'Goodbye'. The execution table traces each step and output. Key points: yield runs the block inside the method, and the method continues after yield. If no block is given, yield causes an error unless checked. Removing yield means the block never runs. This helps write flexible methods that can run custom code blocks.