From: "Kroeger Simon (ext)" Date: 2005-09-28T22:08:21+09:00 Subject: Re: creating independent lambdas in loops > -----Original Message----- > From: itsme213 [mailto:itsme213@hotmail.com] > Sent: Wednesday, September 28, 2005 3:02 PM > To: ruby-talk ML > Subject: Re: creating independent lambdas in loops > > > "ts" wrote in message > news:200509281241.j8SCflhq021177@moulon.inra.fr... > > >>>>> "M" == Michael Roth writes: > > > > M> a = [] > > > > M> for multi in [2, 3, 4, 5] > > M> a << lambda do |n| > > M> n * multi > > M> end > > M> end > > > > [2, 3, 4, 5].each do |multi| > > a << lambda do |n| > > n * multi > > end > > end > > Why the difference in behavior between these two? Because in the second case multi is local to the (do-end) block. So each iteration has its own multi. There is no block if you use for, so the multi in lambda is the same for all iterartions. This is tricky stuff... cheers Simon