From: dblack@... Date: 2007-07-04T23:08:54+09:00 Subject: Re: block params scoping (was ruby-talk 257917: RE: Using a block to surround a string?) ---2049402039-1856756911-1183558136=:10223 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-1856756911-1183558136=:10223" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-1856756911-1183558136=:10223 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Wed, 4 Jul 2007, Gregory Brown wrote: > On 7/4/07, Pe=F1a, Botp wrote: >> Fr David Black: >> # Method definitions always have their own local scope, so a, b, and >> # string are strictly local to the method. Blocks pick up the scope >> # that they're created in, and can also create variables that weren't >> # already in that scope. Those variables disappear when the block >> # exits; the ones that were there already survive: >> # >> # x =3D 1 >> # some_method { y =3D x + 1 } # same x; new y >> # y # undefined; block's y didn't survive >> # x # same x >>=20 >> Hi David, >>=20 >> How about parameter variables, will its scoping change/stay in ruby2 ? >>=20 >> Currently, >>=20 >> irb(main):011:0> x=3D"testing" >> =3D> "testing" >> irb(main):012:0> 5.times{|x|} >> =3D> 5 >> irb(main):013:0> x >> =3D> 4 >>=20 >> i'd prefer >>=20 >> x =3D "testing" >> some_method { |x| y =3D x + 1 } # different x; overrides any x >> x # =3D> "testing", same x outside >>=20 > > Hi, example from Eigenclass[0]: > > a =3D 1 > 10.times{|a| } # !> shadowing outer local variable - a > a # =3D> 1 > > looks like it throws a warning and behaves as we all want it to. :) I actually like the current behavior, but I think I'm in a minority of two (me and Guy Decoux, as I recall). I don't like the warning thing. If the whole point is to make life easy for people who cut-and-paste code blocks and don't want to check the names of the variables (which I don't do, but I gather that's what people want to do), then it should be totally ignored. A warning means you have to go in and change it, which is what you have to do now. So I don't see any gain. David --=20 * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) ---2049402039-1856756911-1183558136=:10223-- ---2049402039-1856756911-1183558136=:10223--