From: Brian Adkins Date: 2008-01-10T09:00:01+09:00 Subject: Re: problem with closure/block On Jan 9, 4:42 pm, "levili...@gmail.com" wrote: > ########################### > y=5 > print "y: " + y.to_s + "\n" > text_at_the_end = lambda do |y| > print y + " text at the end\n" > end > > print "y: " + y.to_s + "\n" > > def oneparam > yield("oneparam") > end > > oneparam(&text_at_the_end) > print "y: " + y + "\n" > > ######################## > It gives the following output: > > y: 5 > y: 5 > oneparam text at the end > y: oneparam > > Is this a bug? Shouldn't "y" be in a local scope in "text_at_the_end"? > I just downloadad Ruby yesterday: > > Ruby Version 1.8.6 > Installer Version 186-26 "However, if at the time the block executes a local variable already exists with the same name as that of a variable in the block, the existing local variable will be used in the block. Its value will therefore be available after the block finishes." "Programming Ruby" p. 106 Whether this is a "bug" or a "feature" is in the eye of the beholder, but it is something you should be aware of. Has this changed in 1.9, or is it anticipated to change in 2.0 ?