From: Yuh-Ruey Chen Date: 2008-11-01T06:18:57+09:00 Subject: Re: How to access to local variables in enclosing scopes? On Oct 31, 3:09 pm, Stefan Lang wrote: > 2008/10/31 Yuh-Ruey Chen : > > > Well then just wrap that example in another function: > > > def scope() > >        a = 20 > >        def foo(): > >                a = 10 > >                def bar(): > >                        print a # prints 10 > >                bar() > >        foo() > >        print a # prints 20 > > scope() > > FWIW, Ruby doesn't have nested procedures like that. According to my copy of Ruby, it does. > > I am trying to understand Ruby with some metaprogramming practice. But > > I still feel that this is a really backwards step in language design. > > > It's like telling me I should use GOTO in original BASIC instead of > > trying to bend the language to simulate while loops. Would any sane > > modern programmer do that? > > I don't think it's a good analogy. Ruby is more oriented towards > little objects and message passing than Pascal-like nested > procedures. Then why the first-class code blocks? Certainly makes Ruby a functional language, if only for blocks. > I'm sure we could work out an elegant solution to some higher > level goal than "accessing local variables", especially when it > comes to metaprogramming. The ability to access local vars in enclosing scopes is strictly unnecessary. But the same could also be said for instance_variable_get/ defined?/set.