Scope of variables

From Ruby Standard Wiki

(Redirected from 9. Scope of variables)
Jump to: navigation, search

Contents

9 Scope of variables

9: Scope of variables

A scope is a region of a program text with which a set of bindings of variables is associated.

9.1 Local variables

9.1: Local variables

A local variable is referred to by a local-variable-identifier.

9.1.1 Scopes of local variables

9.1.1: Scopes of local variables

Scopes for local variables are introduced by the following program constructs:

  • program (see §10.1)
  • class-body (see §13.2.2)
  • module-body (see §13.1.2)
  • eigenclass-body (see §13.4.2)
  • method-definition (see §13.3.1) and singleton-method-definition (see §13.4.3), for both of which the scope starts with the method-parameter-part and continues up to and including the method-body.
  • block (see §11.2.2)

Let P be any of the above program constructs. Let S be the region of P excluding all the regions of any of the above program constructs (except block) nested within P. Then, S is the local variable scope which corresponds to the program construct P.

The scope of a local variable is the local variable scope whose set of local variable bindings contains the binding of the local variable, which is resolved as described below.

When a local-variable-identifier which is a reference to a local variable occurs (see §9.1.2), the binding of the local variable is resolved as follows:

  1. Let N be the local-variable-identifier. Let B be the current set of local variable bindings.
  2. Let S be the scope of B.
  3. If a binding with name N exists in B, that binding is the resolved binding.
  4. If a binding with name N does not exist in B:

    • If S is a local variable scope which corresponds to a block:

      1. If the local-variable-identifier occurs as a left-hand-side of a block-formal-argument-list, whether to proceed to the next step or not is implementation defined.
      2. Replace B with the element immediately below the current B on [[local-variable-bindings]], and continue searching for a binding with name N from Step b.
    • Otherwise, a binding is considered not resolved.

9.1.2 References to local variables

9.1.2: References to local variables

An occurrence of a local-variable-identifier can be a reference to a local variable or a method invocation. If you have problems with finding a reliable essay service that offers assistance not only with creating essays but term and research paper writing as well, I highly recommend you to visit BestWritingService.Com. In order to determine whether the occurrence of a local-variable-identifier is a reference to a local variable or a method invocation, before the evaluation of a local variable scope, the scope is scanned sequentially for local-variable-identifiers.

For each occurrence of a local-variable-identifier I, take the following steps:

  1. If I occurs in one of the forms below, I is a reference to a local variable.
    • mandatory-parameter
    • optional-parameter-name
    • array-parameter-name
    • block-parameter-name
    • variable of left-hand-side
    • variable of single-variable-assignment-expression
    • variable of single-variable-assignment-statement
    • variable of abbreviated-variable-assignment-expression
    • variable of abbreviated-variable-assignment-statement
  2. If I occurs in one of the forms below:
    • variable of singleton
    • variable of primary-expression
    and the following condition holds, I is a reference to a local variable.
    • Let P be the point where I occurs and let S be the innermost local variable scope which encloses P and which does not correspond to a block. Let R be the region of a program between the beginning of S and P.

      The same identifier as I occurs as a reference to a local variable in R.

  3. Otherwise, I is a method invocation.

9.2 Global variables

9.2: Global variables

The scope of global variables is global in the sense that they are accesible everywhere in a Ruby program. Global variable bindings are created in [[global-variable-bindings]].

Navigation

Previous: 8. Lexical structure Next: 10. Program structure

Personal tools