[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: scope questions

Paul Brannan

9/18/2007 2:50:00 PM

On Tue, Sep 18, 2007 at 11:29:25PM +0900, Mister Rohit wrote:
> my $var=1;
> {
> my $var=2;
> print "inner=$var\n"
> }
> print "outer=$var\n"

If the variables really are different, then use a different variable
name. Even though you can do this in perl, that does not make it a good
idea.

Methods should, in general, be short enough that you do not have naming
conflicts between local variables. If you find yourself running into
this a lot, you are probably doing too much in one method, which can
make code harder to read and to refactor later on.

Matz has discussed some syntaxes for what you want for Ruby 2.0, such
as:

var = 1
local { |var|
var = 2
}
puts var #=> 1

(so that block parameters are always local to the block) but I don't
know what the current status of this is.

Paul