0
0
Rubyprogramming~20 mins

Closures and variable binding in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Closure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Ruby closure code?

Consider the following Ruby code using closures and variable binding. What will it print?

Ruby
def make_procs
  procs = []
  3.times do |i|
    procs << Proc.new { i }
  end
  procs
end

procs = make_procs
procs.each { |p| puts p.call }
A
3
3
3
B
0
1
2
C
0
0
0
D
1
2
3
Attempts:
2 left
💡 Hint

In Ruby, the block variable i is captured by value in each iteration, so each Proc remembers its own i.

Predict Output
intermediate
2:00remaining
What does this Ruby closure print when variables change after creation?

Look at this Ruby code where a closure captures a variable that changes after the closure is created. What is printed?

Ruby
def counter
  count = 0
  Proc.new { count += 1 }
end

c = counter
puts c.call
puts c.call
count = 100
puts c.call
A
1
2
2
B
1
2
100
C
0
1
2
D
1
2
3
Attempts:
2 left
💡 Hint

The closure captures the local variable count inside the method, not the top-level count.

🔧 Debug
advanced
2:00remaining
Why does this Ruby closure print the same value three times?

Examine this Ruby code. It creates an array of Procs inside a loop, but all Procs print the same value. Why?

Ruby
procs = []
value = 0
3.times do
  procs << Proc.new { value }
  value += 1
end
procs.each { |p| puts p.call }
ABecause all Procs share the same <code>value</code> variable, which ends at 3
BBecause the block variable is not captured correctly
CBecause Procs are executed immediately in the loop
DBecause <code>value</code> is reset inside the loop each time
Attempts:
2 left
💡 Hint

Think about what variable the Proc actually remembers and when it is evaluated.

📝 Syntax
advanced
2:00remaining
Which option correctly creates a closure capturing loop variable in Ruby?

Which of these code snippets correctly creates an array of Procs that each remember a different loop variable value?

A
procs = []
3.times do |i|
  procs &lt;&lt; Proc.new { i }
end
B
procs = []
3.times do |i|
  procs &lt;&lt; Proc.new { i += 1 }
end
C
procs = []
3.times do |i|
  x = i
  procs &lt;&lt; Proc.new { x }
end
D
procs = []
3.times do |i|
  procs &lt;&lt; Proc.new { |x| i }
end
Attempts:
2 left
💡 Hint

Try to create a new variable inside the loop to capture the current value.

🚀 Application
expert
2:00remaining
How many items are in the resulting array after this Ruby closure code runs?

Analyze this Ruby code that uses closures and variable binding. How many elements does the results array contain after execution?

Ruby
def create_procs
  procs = []
  i = 0
  while i < 3 do
    procs << Proc.new { i }
    i += 1
  end
  procs
end

results = create_procs.map(&:call)
A3
B1
C0
DRaises an error
Attempts:
2 left
💡 Hint

Count how many Procs are added to the array inside the loop.