From: Robert Klemme Date: 2006-03-27T22:23:55+09:00 Subject: Re: scope issue of variable in iterator Mauricio Fernandez wrote: > On Mon, Mar 27, 2006 at 04:23:54PM +0900, Robert Klemme wrote: >> Mohammad Khan wrote: >>> When I am writing >>> array.each do | blah | end >>> >>> It would be really convenient if blah is scopped only in this iterator >>> block, regardless of blah decleared before or not. >> You wouldn't be able to do things like this: >> >> sum = 0 >> enum.each {|item| sum += item} > > Not really; item being block-scoped wouldn't preclude access to other local > variables. Right. Apparently I wasn't fully awake when I wrote that. > What you wouldn't be able to do is: > > def set_get > a = nil > [lambda{|a|}, lambda{a}] > end > set, get = set_get > set[1] > get[] # => 1 > > forcing you to go this way: > [lambda{|b| a = b}, lambda{a}] Seems like a rare thing to do. This is another thing you cannot do with the proposed change: last = nil enum.each {|last|} puts "last was #{last}" Although you can of course with #inject. Kind regards robert