Leo Razoumov wrote:
> 
> 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.
> ...

This same request was brought up by Ben Tilly not too long ago.
(Posted as #U006 at http://www.rubygarden.org/ruby?NeedDiscussion)

Furthermore, a very long thread was conducted last Oct. 
You can find the 'restart' at  [ruby-talk:5552]
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/5552


> 
> # 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?
> 

My first thought is, of course, this has been brought up already.

My second thought is that, yes, something probably needs to be done.

My third thought is, please don't use 'my' to implement this.
For one thing, what was tacked on in Perl is not likely to be
the best way for Ruby to handle it. Not to mention that x is already
a local variable. Of course, the problem is that Ruby might seem 
to have to be tacking something on in any case.  Ruby will
have to either create a new kind of scope, or nesting, or naming.

This particular nuance of scoping in Ruby is hard to grasp...
I am not sure there will be an answer any time soon.

This is perhaps a good place to expose some of the complexity of Ruby.

By the way, blocks don't work in Ruby the same as they do in Perl.

In any case, since the October thread, I have come to actually like
the way things are. I haven't ever had to worry about previously
named variables. In fact, all this worry about overlapping scope
seems so far-fetched to me now. In practice, there is hardly ever
any need to maintain such large numbers of local vars that might
conflict because 1) it is easy to modularize Ruby code, 2) it
is easy to protect namespaces, and 3) it is easy to modularize Ruby
code. (I may be repeating myself).

So apart from the fact that I spent so many hours trying to figure it all
out back then, I haven't had much to worry about now that I grasp it better.
I have just seen too many other areas where it seems to make up for
the inconvenience of having to initialize in advance and otherwise control
the namespace.

I do think it would be interesting to have some kind of class that allowed
you to create 'shadowed' variables, though...


Guy N. Hurst



-- 
HurstLinks Web Development    http://www.hurstlinks.com/
Norfolk, VA  23510            (757)623-9688 FAX 623-0433
PHP/MySQL - Ruby/Perl - HTML/Javascript