Jump to content

Closures/Value capture: Difference between revisions

→‎{{header|Ruby}}: Add a few sentences to explain when i is local.
(→‎{{header|Common Lisp}}: Add 3 sentences to describe this code.)
(→‎{{header|Ruby}}: Add a few sentences to explain when i is local.)
Line 596:
irb(main):003:0> list[7][]
=> 49</lang>
 
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.
 
With Ruby 1.9, ''i'' is always local to its block. With Ruby 1.8, ''i'' is local unless there is another ''i'' in the outer scope. If ''i'' is not local, all 10 procs will return 100.
 
=={{header|Smalltalk}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.