From: Leo Razoumov Date: 2001-03-07T13:10:02+09:00 Subject: [ruby-talk:12204] FEATURE REQUEST: 'my' local variables Ruby is, indeed, a very well designed language. But here is one aspect of Ruby's scoping rules which might be worth of some improvement. At present (Ruby-1.6.2) the Ruby scoping rules stipulate that inside a block (either {...} or do...end) a new local variable is created _ONLY_ if _NO_ such a variable of the same name exists in any of the enclosing scopes. On the other hand if there is already such a varible in an enclosing scope it will used in the inner block instead of creating a new variable. This rule is very convenient in many cases. For example: x = 1 { x = 7 # the 'x' binding is reused from the outer scope y = 0 # new variable 'y' (local to this block) created } x # -> 7 y # raise NameError: undefined local variable On the other hand in some cases one wants to be able to create a local variable in the inner block even though a variable with the same name already exists in one of the enclosing scopes. # PROPOSED FEATURE: x = 0 #line_1 { my x = 7 # new varible 'x' shadowing 'x' from line_1 x # -> 7 } x # -> 0 I guess it should not be hard to add this functionality to the language. Any thoughts? Thanks, --Leo