"Ruby Variable Scope"

What is Variable Scope?


Scope defines where in a program a variable is accessible. Ruby has four types of variable scope,local, global, instance and class. In addition, Ruby has one constant type. Each variable type is declared by using a special character at the start of the variable name as outlined in the following table.

<table border=1 cellspacing=0><tbody><tr style=”background: none repeat scroll 0% 0% #efefef;”><th>Name Begins With</th>
<th>Variable Scope</th>
<td>A global variable</td>
<td>An instance variable</td>
</tr><tr><td>[a-z] or _</td>
<td>A local variable</td>
<td>A constant</td>
<td>A class variable</td>

In addition, Ruby has two pseudo-variables which cannot be assigned values. These are nil which is assigned to uninitialized variables and self which refers to the currently executing object. In the remainder of this chapter we will look at each of these variable scopes in turn.


[edit] Detecting the Scope of a Ruby Variable

Obviously, you can tell the scope of a variable by looking at the name. Sometimes, however, you need to find out the scope programmatically. A useful technique to find out the scope of a variable is to use the defined? method. defined? will return the scope of the variable referenced, or nil if the variable is not defined in the current context:

x = 10
=> 10
defined? x
=> "local-variable"

$x = 10
=> 10
defined? $x
=> "global-variable"


 Ruby Local Variables

Local variables are local to the code construct in which they are declared. For example, a local variable declared in a method or within a loop cannot be accessed outside of that loop or method. Local variable names must begin with either an underscore or a lower case letter. For example:

loopcounter = 10
_LoopCounter = 20


 Ruby Global Variables

Global variables in Ruby are accessible from anywhere in the Ruby program, regardless of where they are declared. Global variable names are prefixed with a dollar sign ($). For example:

$welcome = "Welcome to Ruby Essentials"

Use of global variables is strongly discouraged. The problem with global variables is that, not only are they visible anywhere in the code for a program, they can also be changed from anywhere in the application. This can make tracking bugs difficult.

It is useful to know, however, that a number of pre-defined global variables are available to you as a Ruby developer to obtain information about the Ruby environment. A brief summary of each of these variables is contained in the following table.

<table border=1 cellspacing=0><tbody><tr style=”background: none repeat scroll 0% 0% #efefef;”><th>Variable Name</th>
<th>Variable Value</th>
<td>The location of latest error</td>
<td>The string last read by gets</td>
<td>The line number last read by interpreter</td>
<td>The string last matched by regexp</td>
<td>The last regexp match, as an array of subexpressions</td>
<td>The nth subexpression in the last match (same as $~[n])</td>
<td>The case-insensitivity flag</td>
<td>The input record separator</td>
<td>The output record separator</td>
<td>The name of the ruby script file currently executing</td>
<td>The command line arguments used to invoke the script</td>
<td>The Ruby interpreter’s process ID</td>
<td>The exit status of last executed child process</td>

For example we can execute the gets method to take input from the keyboard, and then reference the $_ variable to retrieve the value entered:

irb(main):005:0> gets
=> "hellon"
irb(main):006:0> $_
=> "hellon"

Alternatively we could find the process ID of the Ruby interpreter:

irb(main):007:0> $$
=> 17403


Ruby Class Variables

A class variable is a variable that is shared amongst all instances of a class. This means that only one variable value exists for all objects instantiated from this class. This means that if one object instance changes the value of the variable, that new value will essentially change for all other object instances.

Another way of thinking of thinking of class variables is as global variables within the context of a single class.

Class variables are declared by prefixing the variable name with two @ characters (@@). Class variables must be initialized at creation time. For example:

@@total = 0


 Ruby Instance Variables

Instance variables are similar to Class variables except that their values are local to specific instances of an object. For example if a class contains an instance variable called @total, if one instance of the object changes the current value of @total the change is local to only the object that made the change. Other objects of the same class have their own local copies of the variable which are independent of changes made in any other objects.

Instance variables are declared in Ruby by prefixing the variable name with a single @ sign:

@total = 10


  Ruby Constant Scope

Ruby constants are values which, once assigned a value, should not be changed. I say should because Ruby differs from most programming languages in that it allows a constant value to be changed after it has been declared, although the interpreter will protest slightly with a warning message.

Constants declared within a class or module are available anywhere within the context of that class or module. Constants declared outside of a class or module are assigned global scope.

"Ruby Variable Scope"

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s