2008/10/31 Yuh-Ruey Chen <maian330 / gmail.com>:

> The difference that the local variable doesn't have to be defined in
> global scope. Only child scopes should have access to that local
> variable.
>
> def foo
>        a = 10
>        def bar
>                # should somehow be able to access a
>        end
>        bar
> end
> foo
> # here, we should not be able to access foo's a, but if there is
> another a in scope, we can access that
>
> Or to put it in Python:
>
> a = 20
> def foo():
>        a = 10
>        def bar():
>                print a # prints 10
>        bar()
> foo()
> print a # prints 20

The first assignment to a creates a _module_ scoped variable in Python.
It is _not_ local, it can very well be accessed from anywhere via

    import some_module
    some_module.a

Since it's a bad idea anyway to reassign such variables
(metaprogramming might be an exception), you can use
Ruby's constants for this purpose:

    module MyModule

        A = 20

        # can access A from anywhere within MyModule,
        # including method definitions, nested classes, etc.

    end

    puts MyModule::A  # access from outside of MyModule

At least anything that looks like local variable assignment
in Ruby is actually local variable assignment. The thing to
learn is that "module", "class" and "def" create new, empty
local scopes, they have no enclosing scope with regards to
local variables.

It is generally a better idea to genuinely try to understand
a new language and its idioms instead of bending it to ones
expectations from the beginning.

Stefan