From: Brian Candler Date: 2003-02-06T16:56:20+09:00 Subject: Re: Local variables & blocks I had another thought. It would be wonderfully consistent if *all* variables assigned in blocks were block-local, and some people have expressed this, e.g. [ruby-talk:63516]. But we still want to be able to bind to variables outside. How about it you thought of them as a special type of parameter to the block? Perhaps something like this: foo = 1 bar = 2 myobj.each { |i| ... ... Any use of 'foo' or 'bar' is bound to the outer scope ... All other variables are block-local } Now, an 'import' declaration has been suggested before, but the above looks more like a new type of block parameter, bound statically to the enclosing scope instead of the arguments to call(), and therefore might pass the "no declarations" rule. There is the problem of backward compatibility to sort out, since using that particular syntax in new scripts would barf under old Ruby interpreters. There might be some tricks though, what if you chose: foo = 1 bar = 2 myobj.each { |i| [foo,bar] ... } This is overloading, but a free-standing array literal whose value is not used, as the first line of a block, is unlikely to be useful otherwise. It doesn't quite work if you write your block all on one line: myobj.each { |i| [foo,bar] puts i } # no, not under current Ruby myobj.each { |i| [foo,bar]; puts i } # this is OK Forward compatibility is an issue though, as any code written under old Ruby which binds to variables outside a block would have to be modified to run under new Ruby. This is where a shadowing warning would be really useful: # some old code found = "no" myobj.each { |i| ... found = "yes" # WARN: this is now block-local and shadows 'found' } But perhaps this suggestion falls in the category of "things which we might do if we were redesigning the language from scratch" Regards, Brian.