Difference between revisions of "Ruby Variable Scope"

From Techotopia
Jump to: navigation, search
(What is Variable Scope?)
(What is Variable Scope?)
Line 18: Line 18:
 
<code>@@</code></td><td>A class variable</td></tr></table>
 
<code>@@</code></td><td>A class variable</td></tr></table>
  
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 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.
 +
 
 +
== 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 variables name must begin with either an underscore or a lower case letter. For example:
 +
 
 +
<pre>
 +
loopcounter = 10
 +
_LoopCounter = 20
 +
</pre>
 +
 
 +
== 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:
 +
 
 +
<pre>
 +
$welcome = "Welcome to Ruby Essentials"
 +
</pre>
 +
 
 +
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 cna 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 enviroment. A brief summary of each of these variables is contained in the following table.
 +
 
 +
<table border="1"><tr><td><code>$!</code> </td><td> The latest error message </td></tr>
 +
<tr><td>
 +
<code>$@</code> </td><td> The location of latest error </td></tr>
 +
<tr><td>
 +
<code>$_</code> </td><td> The string last read by <code>gets</code> </td></tr>
 +
 
 +
<tr><td>
 +
<code>$.</code> </td><td> The line <em>number</em> last read by interpreter </td></tr>
 +
<tr><td>
 +
<code>$&amp;</code> </td><td> The string last matched by regexp </td></tr>
 +
<tr><td>
 +
<code>$~</code> </td><td> The last regexp match, as an array of subexpressions </td></tr>
 +
 
 +
<tr><td>
 +
<code>$</code><em>n</em> </td><td> The <em>nth</em> subexpression in the last match (same as <code>$~[</code><em>n</em><code>]</code>) </td></tr>
 +
<tr><td>
 +
<code>$=</code> </td><td> The case-insensitivity flag </td></tr>
 +
 
 +
<tr><td>
 +
<code>$/</code> </td><td> The input record separator </td></tr>
 +
<tr><td>
 +
<code>$\</code> </td><td> The output record separator </td></tr>
 +
<tr><td>
 +
<code>$0</code> </td><td> The name of the ruby script file currently executing</td></tr>
 +
 
 +
<tr><td>
 +
<code>$*</code> </td><td> The command line arguments used to invoke the script</td></tr>
 +
<tr><td>
 +
<code>$$</code> </td><td> The Ruby interpreter's process ID </td></tr>
 +
<tr><td>
 +
<code>$?</code> </td><td> The exit status of last executed child process</td></tr></table>

Revision as of 18:56, 15 November 2007

Now that we have covered the basics of variables in Ruby the next task is to explain 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.

Name Begins WithVariable Scope
$ A global variable
@ An instance variable
[a-z] or _ A local variable
[A-Z] A constant
@@A class variable

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.

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 variables name 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 cna 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 enviroment. A brief summary of each of these variables is contained in the following table.

$! The latest error message
$@ The location of latest error
$_ The string last read by gets
$. The line number last read by interpreter
$& The string last matched by regexp
$~ The last regexp match, as an array of subexpressions
$n The nth subexpression in the last match (same as $~[n])
$= The case-insensitivity flag
$/ The input record separator
$\ The output record separator
$0 The name of the ruby script file currently executing
$* The command line arguments used to invoke the script
$$ The Ruby interpreter's process ID
$? The exit status of last executed child process