From: cjon@... (Cullen J O'neill) Date: 2001-03-13T08:10:07+09:00 Subject: [ruby-talk:12533] Re: FEATURE REQUEST: 'my' local variables Stephen White (spwhite@chariot.net.au) wrote: : On Mon, 12 Mar 2001, W. Kent Starr wrote: : > Howabout creating a "strict" local using a prefix in front of an alpha : > (excepting symbols like $) as in _var to mean LOCAL! LOCAL!! LOCCAL!!! : > as in : : > : > x = 5; loop { |y| x = 3; x * y }; puts x ->5 : > x = 5; loop { |y| _x = 3; _x * y }; puts x -> 3 : I think this is the best suggestion I've seen so far, though it needs : to be better defined to cover this scenario: : x = 5; loop {|y| _x = x + 5; loop {|y| puts _x } } : Does the _x carry over from the parent loop? What about the reverse: x = 5; loop { |y| x = 3; x * y }; puts x ->3 x = 5; loop { |y| _x = 3; _x * y }; puts x -> 5 This is just like normal code. and x = 5; loop {|y| _x = x + 5; loop {|y| puts _x } } -> 10 and _x = 5; loop {|y| _x = x + 5; loop {|y| _x = 12 }; puts _x } ; puts _x -> 10 5 (where the first puts will output 10 and the second 5) _ at the beginning basically would mean that the variable will be shadowed by default (in blocks anyway) Cullen J O'Neill -- cjon@engin.umich.edu