From: matz@... (Yukihiro Matsumoto) Date: 2003-01-30T22:26:31+09:00 Subject: Re: Local variables & blocks Hi, In message "Re: Local variables & blocks" on 03/01/30, YANAGAWA Kazuhisa writes: |So in the following: | | # until here, there's no foo. | 10.times do | Thread.new do | # long, time-varying process without foo. | | foo = Foo.new | | # process using foo. | end | end | |Do all new threads (and of course the main thread) share variable foo |and their accesses are interleaved arbitrary? Well in that case we |still use block parameter as block local variable but that's little |quirky. Yes. You will need to introduce block local variables explicitly, for example 10.times do Thread.new do # long, time-varying process without foo. local do |foo| # "local" is merely a method name foo = Foo.new # process using foo. end end end or maybe more compactly 10.times do Thread.new do # long, time-varying process without foo. let foo = Foo.new # foo is available to the end of scope # process using foo. end end |I heard once that that is one of current behavior --- a variable |appears first in a block, that variable is localized for the block. It is true (the primary reason was to implement lambda). But result was not as good as I expected. matz.