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

From: schneik@...
Date: 2000-04-18 19:58:38 UTC
List: ruby-talk #2506

Dave Thomas writes:

# I'm trying to define Ruby's various scoping rules as concisely as
# possible for the language section of the book.

I think that should be very useful for Ruby novices.

<...>

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

I think "a program" would be better than "an application",
which may involve several programs behind the scenes. Plus you
use "program" below.

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

I think the word "scope" somehow wants to be used here instead of "are
available".  Do you (also) mean/want to say/imply: "Instance variables
are local to a class body."?  Does an "unitialized instance variable"
actually exist prior to or after a reference to it?

#      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.

Consider something like "Local variables are created dynamically when
first assigned to during program execution.  However the scope of
local variables is determined statically (so independent versions of
local variables with identical names may exist in different scopes)."

#      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.

Can "considered to be" be considered to be unnecessary here, or is
there a more subtle distinction here?

#      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.

How about "The binding preserves these variable[s], even if...."?

#      \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.

This seems a superfluous distraction: "are built in to the language
and".  Instead of describing this negatively, I would just say that
they are part of the enclosing scope.

Conrad Schneiker
(This note is unofficial and subject to improvement without notice.)


In This Thread

Prev Next