From: "Jesús Gabriel y Galán" Date: 2010-05-06T01:01:24+09:00 Subject: Re: for-in vs. map closures On Wed, May 5, 2010 at 5:45 PM, Josh Cheek wrote: > Wow, those were both really good explanations. Thanks David and Jesus. In fact there was something not so clear (or just plain wrong, depending who you ask :-), in my explanation. I said: > Here n is the same in all iterations (it's outside the do ... end), > for defines it in the scope that is above the block The fact that n is outside the do...end has nothing to do with the rest of the explanation. As David said, for doesn't start a new scope, so even if it were like this it wouldn't yield different results: irb(main):008:0> a = [] => [] irb(main):009:0> for i in [1,2] do irb(main):010:1> value = i irb(main):011:1> a << lambda {value * value} irb(main):012:1> end => [1, 2] irb(main):013:0> a.each {|l| puts l.call} 4 4 Even though value is inside the block, as the for keyword doesn't create a new scope, value is defined in the outer scope. So, what's here between the do...end is not a regular ruby block, in fact, the do is optional: irb(main):008:0> a = [] => [] irb(main):009:0> for i in [1,2] irb(main):010:1> value = i irb(main):011:1> a << lambda {value * value} irb(main):012:1> end => [1, 2] irb(main):013:0> a.each {|l| puts l.call} 4 4 After either of those: irb(main):020:0> defined? value => "local-variable" Jesus.