Changes

Ruby Variable Scope

372 bytes added, 19:19, 15 November 2007
What is Variable Scope?
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.
 
== Detecting the Scope of a Ruby Variable ==
 
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:
 
<pre>
x = 10
=> 10
defined? x
=> "local-variable"
 
$x = 10
=> 10
defined? $x
=> "global-variable"
</pre>
== Ruby Local Variables ==