5.2. Determining scope

Scope refers to the reach or visibility of identifiers, specifically variables and constants. Different types of identifiers have different scoping rules; using, say, the identifier x for a local variable in each of two method definitions has a different effect than using the global variable $x in the same two places, because local and global variables differ as to scope. In this section, we’ll consider three types of variables: global, local, and class variables. (As you’ve just seen, instance variables are self-bound, rather than scope-bound.) We’ll also look at the rules for resolving constants.

Self and scope are similar in that they both change over the course of a program, and in that you can deduce what’s going on ...

Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.