Closures/Value capture: Difference between revisions

Content added Content deleted
(→‎{{header|Common Lisp}}: Add 3 sentences to describe this code.)
(→‎{{header|Ruby}}: Add a few sentences to explain when i is local.)
Line 596: Line 596:
irb(main):003:0> list[7][]
irb(main):003:0> list[7][]
=> 49</lang>
=> 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}}==
=={{header|Smalltalk}}==