On Thu, 30 Nov 2000  08:10:01 +0900, Steven Grady wrote:
> As I am still learning the language, I am hitting things which are
> confusing.  I thought I'd share one with people here -- perhaps it
> may spark some conversation as to whether it violates POLS.
> 
> In essence, one problem I had came down to the following:
> 
>     procs = []
>     for v in [1, 2]
> 	procs << proc { v }
>     end
>     procs.collect { |p| p.call }	-> [2, 2]
> 
> I was hoping for [1, 2].
[snip]
> After some exploration (and reading a bit of the book), I discovered that
> an alternate solution is:
>     procs = []
>     [1, 2].each { |v|
> 	procs << proc { v }
>     }   
>     procs.collect { |p| p.call }	-> [1, 2]

So far so good. Now try running these snippets after each other:

-> [2,2]
-> [2,2]

Does this relate to the block-local variables discussion earlier? If
run separate, the 2nd script has a local v inside the block. If run
after each other, the v inside the 2nd block refers to the globally
defined v, and thus the lambdas refer to the same objects. This can be
validated by returning v.id instead of v.

Would, a construct similar to Thread.new,

	proc([arg*]) { |args| block } -> aProc

reduce the surprise? (I may overlook some binding semantics here, just
an idea)

	Michel