0
0
Rubyprogramming~10 mins

Block given? check in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Block given? check
Method called
Check if block given?
Yield block
Continue method
End
The method checks if a block is given. If yes, it runs the block with yield; if no, it skips yield and continues.
Execution Sample
Ruby
def greet
  if block_given?
    yield
  else
    puts "No block given"
  end
end

greet { puts "Hello!" }
This code runs a method that prints 'Hello!' if a block is given, otherwise prints 'No block given'.
Execution Table
StepActionblock_given? checkBranch TakenOutput
1Call greet with blocktrueYes (yield block)Hello!
2Yield block execution--Hello! printed
3Method ends---
💡 Block was given, so yield executed and method ended.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
block_given?falsetruetruetrue
Key Moments - 2 Insights
Why does the method not print 'No block given' when a block is passed?
Because block_given? returns true (see execution_table step 1), so the method yields to the block instead of printing the else message.
What happens if we call greet without a block?
block_given? returns false, so the else branch runs and prints 'No block given' (not shown in this trace but implied).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of block_given? at step 1?
Afalse
Btrue
Cnil
Dundefined
💡 Hint
Check the 'block_given? check' column in execution_table row for step 1.
At which step does the method yield to the block?
AStep 2
BStep 3
CStep 1
DNo yield occurs
💡 Hint
Look at the 'Action' and 'Branch Taken' columns in execution_table.
If greet is called without a block, what would change in the execution_table?
Ablock_given? would be true and yield runs
BMethod would raise an error
Cblock_given? would be false and else branch runs
DOutput would be 'Hello!'
💡 Hint
Refer to key_moments about behavior when no block is given.
Concept Snapshot
Use block_given? inside a method to check if a block was passed.
If true, use yield to run the block.
If false, skip yield or run alternative code.
Prevents errors when yield is called without a block.
Syntax:
  if block_given?
    yield
  else
    # no block code
  end
Full Transcript
This example shows how Ruby methods can check if a block is given using block_given?. When the method greet is called with a block, block_given? returns true, so the method yields to the block and prints 'Hello!'. If no block is given, block_given? returns false and the method prints 'No block given'. This prevents errors from calling yield without a block. The execution table traces each step: calling the method, checking block_given?, yielding the block, and ending the method. The variable tracker shows block_given? changing from false to true when the block is present. Key moments clarify why the else branch is skipped when a block exists and what happens if no block is passed. The quiz tests understanding of block_given? values, when yield runs, and behavior without a block. The snapshot summarizes how to safely use yield with block_given? in Ruby methods.