0
0
Rubyprogramming~10 mins

Closures and variable binding in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Closures and variable binding
Define outer method
Create inner closure (block/lambda)
Inner closure captures outer variable
Return or call closure
Closure uses captured variable
Variable binding shows closure remembers value
This flow shows how a closure captures variables from its surrounding method and remembers their values even after the method ends.
Execution Sample
Ruby
def make_counter
  count = 0
  -> { count += 1 }
end

counter = make_counter
puts counter.call
puts counter.call
This Ruby code defines a closure that remembers and updates a variable 'count' each time it is called.
Execution Table
StepActionVariable 'count'Closure Created?Output
1Enter make_counter methodcount = 0No
2Create lambda closure capturing 'count'count = 0Yes
3Return closure from make_countercount = 0Yes
4Call closure first timecount = 1Yes1
5Call closure second timecount = 2Yes2
💡 Closure calls stop; 'count' variable is updated and remembered inside closure.
Variable Tracker
VariableStartAfter 1st callAfter 2nd callFinal
count0122
Key Moments - 2 Insights
Why does the variable 'count' keep its value between calls to the closure?
Because the closure captures the variable 'count' from the outer method and remembers its state, as shown in execution_table rows 4 and 5 where 'count' increments and keeps its value.
Does the variable 'count' reset to 0 each time we call the closure?
No, it does not reset because the closure holds the binding to the original 'count' variable created in make_counter, so it updates the same variable instead of creating a new one (see execution_table rows 4 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after the first call to the closure?
A1
B0
C2
DUndefined
💡 Hint
Check the 'Variable count' column at step 4 in the execution_table.
At which step is the closure created that captures the variable 'count'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Closure Created?' column in the execution_table.
If we called the closure a third time, what would be the value of 'count'?
A2
B3
C0
DError
💡 Hint
See how 'count' increments by 1 each call in the variable_tracker.
Concept Snapshot
Closures capture variables from their surrounding method.
They remember and update these variables even after the method ends.
In Ruby, lambdas or blocks can act as closures.
Variable binding means the closure keeps the original variable, not a copy.
Each call to the closure can change the captured variable's value.
Full Transcript
This example shows how Ruby closures work by capturing a variable from an outer method. The method make_counter defines a variable 'count' starting at 0 and returns a lambda closure that increments 'count' by 1 each time it is called. The closure remembers the variable 'count' and its updated value between calls. The execution table traces each step: defining 'count', creating the closure, returning it, and calling it twice. The variable tracker shows how 'count' changes from 0 to 1, then 2. Key moments clarify that the closure holds the variable binding, so 'count' does not reset. The quiz tests understanding of when the closure is created, the value of 'count' after calls, and predicting future values. This helps beginners see how closures keep state in Ruby.