From: "Thomas B." Date: 2008-09-11T17:34:28+09:00 Subject: Re: Blocks and scoping question Mischa Fierer wrote: > Hello, > > The following makes sense to me: > > lambda {|x| puts x; [1,2].collect{|x| x+1} }.call("aaa") > aaa > => [2, 3] > > The following does not so much: > > lambda {|x| puts x; [1,2].collect{|x| x+1}; puts x }.call("aaa") > aaa > 2 > => nil > > Why is the final puts x not return a? It's because the x in collect{|x| ...} in fact uses the variable x that previously contained your "aaa", because it is already defined in this scope. If x wasn't defined at the point of calling collect, then x inside collect's block would be a separate variable each iteration. This is going to change in Ruby 1.9, and block variables will always be separate from variables visible in the scope. TPR. -- Posted via http://www.ruby-forum.com/.