[ruby-talk:02505] Variable scope -does this make sense?

From: Dave Thomas <Dave@...>
Date: 2000-04-18 18:05:33 UTC
List: ruby-talk #2505

I'm trying to define Ruby's various scoping rules as concisely as
possible for the language section of the book. I've come up with the
following somewhat dense prose.

My question: does it make sense (in the context of a terse reference
manual)? Is it accurate? Is there more to say?

Many thanks, folks

Dave



     Global variables are available throughout an
     application. Reference to an uninitialized global variable
     returns nil.

     Instance variables are available throughout a class
     body. Reference to an uninitialized instance variable returns
     nil.

     Local variables are unique as their scopes are statically
     determined, but their existence is established dynamically.

     A local variable is created when it is first assigned to during
     program execution.

     The scope of a local variable is the immediately enclosing
     method definition, class definition, module definition, or
     program.

     If a local variable is first assigned in an iterator block, then
     it is local to the block. If a variable of the same name is
     already established at the time the block executes, the block
     will inherit that variable.

     Method parameters and block parameters are considered to be local
     variables, and are assigned to when the method or block is
     invoked.

     When a block is converted into an object of class \Ci{Proc}, it
     takes on the set of local variables in existence at the time the
     object is created. This forms part of the object's binding. Note
     that although the binding of the variables is fixed at this
     point, the procedure object will have access to the current
     values of these variables when it executes. The binding ensures
     that these variable will continue to exist even if the original
     enclosing scope is destroyed.

     \KW{while}, \KW{until}, and \KW{for} loops are built in to the
     language and do not introduce new scope; previously existing
     locals can be used in the loop, and any new locals created will
     be available afterwards.

In This Thread

Prev Next