Hi,

In message "Re: Thought question: Where does "new" come from?"
    on 02/08/16, "MikkelFJ" <mikkelfj-anti-spam / bigfoot.com> writes:

|I didn't track this thread, but today I independently tried to write up the
|scoping rules for Ruby. I think they are very complex and difficult to
|explain. Any given expression is working in a context of about 5 different
|kinds of scope at the same time.

I admit Ruby's scope rules are rather complex, comparing to other part
of Ruby.  But things are much simpler than you described.  The key is
the scope of the local variables.

Global variables has no scope.  You can access them from everywhere.

Instance variables belong to the current value of "self".  "self" may
be switched via instance_eval() etc.

Local variables belong to the current "scope", which is introduced by
the following statements: class, module, def.  In addition, the
toplevel (outside of any of statements above) has its own scope, and
blocks associated with methods have scopes too.  Local variabls are
effective from the first assignment in the current scope to the end of
the scope.  Local variables that belong to the outer scope cannot be
accessed, *except if the current scope is introduced by the block*
(the most complex part of the rules).

Constants are searched in the following order.  The innermost
class/module, outer class/module to the top, superclasses of the
innermost class.

Class variables belong to the innermost *non singleton* class.
(the behavior has changed in 1.6.8 and later;  this is new one).

							matz.