Anonymous user
Closures/Value capture: Difference between revisions
→{{header|Ruby}}
(Added Racket) |
|||
Line 810:
=={{header|Ruby}}==
<lang ruby>
(1..10).each {|i| list[i] = proc {i * i}}
p list[7][] #=> 49
i = 5
p list[3].call #=>
This works because ''i'' in <code>(1..10).each {|i| ...}</code> is local to its block. The loop calls the block 10 times, so there are 10 different variables to capture.
Line 822:
However, (on both Ruby 1.8 and 1.9) when using a for loop, the loop variable is shared and not local to each iteration:
<lang ruby>
for i in 1..10 do list[i] = proc {i * i} end
i = 5
p list[3][] #=> 25</lang>
=={{header|Scheme}}==
|