From: Kevin Smith Date: 2001-03-16T16:33:19+09:00 Subject: [ruby-talk:12725] Re: FEATURE REQUEST: 'my' local variables Hal E. Fulton wrote: >From: Doug Edmunds >> I an new to Ruby, but there seems to be something >> quite wrong with temporary variables inside blocks >> having any impact outside the block. > >That's like asking, "Have you quit beating your wife?" > >The fact is these aren't temporary variables. They might look like them; >some might assume they are; they might sometimes behave as such; >but they aren't. Some might even argue they should be; but they aren't. > >On the other hand, I sympathize with you. The behavior in this case is >a little surprising and hard to get used to. The rule I follow for now is: >Don't name a block parameter the same as an existing variable unless >there is actually a reason to do so. Along those lines, I'm increasingly of the belief that we shouldn't change this feature right now. While I'm not too happy about the existing rules, they are usable. So here's my proposal, which is similar to what someone else suggested a week ago (sorry--I forget who it was): Ruby should generate a warning whenever a block variable will recycle an existing variable. People should be discouraged from using this "feature", except when needed as a speed optimization. Ideally, there would be some way to disable this warning on a case-by-case basis. Perhaps via a mechanism similar to a C++ pragma, and possibly this could take the form of a comment. a = 2 # the following line will disable the warning, # either for this block, or maybe for all # following blocks. #block_reuse(a) 1.times { a = 5 } Kevin