Variables and Constants

The type of variable or constant in Ruby--local variables, instance variables, class variables, global variables, and constants--can be determined from its initial character. Normally a variable has an alphanumeric name (outside of its first character) which can include an underscore, but some built-in variables begin with '$' + one character (see Built-In Variables).

Variables and constants point to specified objects. Assigning objects to variables and constants simply makes them point to new objects; it does not create new copies of objects.

Local Variables

Example:

foobar

Identifiers beginning with a lowercase letter or '_' are local variables or method calls.

Within a local variable's scope (classes, modules, method definitions), the initial assignment to an identifier beginning with lowercase letters is the declaration of that scope's local variable. Referencing undeclared identifiers is considered a call to a method with no arguments.

Instance Variables

Example:

@foobar

Variables beginning with '@' are instance variables and belong to specific objects. Instance variables can be referenced from any method of their class or subclass. When referenced, the value of an uninitialized instance variable is nil.

Class Variables

Example:

class Foo
  @@foo = 1
  def bar
    puts @@foo
  end
end

Variables beginning with @@ are class variables. Class variables are defined within the class definition and can be referenced/assigned from class-unique methods, instance methods, etc.

The differences between class variables and constants are as follows.

Global Variables

Example:

$foobar

Variables beginning with '$' are global variables and can be referenced from anywhere in the program (thus requiring some caution when using). Global variables do not need declarations. When referenced, the value of an uninitialized global variable is nil.

Pseudo Variables

Apart from the usual variables, there are also special variables known as pseudo variables.

self

The current method's execution constituent.

nil

The only instance of the NilClass class. Signifies NIL.

true

The only instance of the TrueClass class. Signifies TRUE.

false

The only instance of the FalseClass class. Signifies FALSE.

The value of a pseudo variable cannot be changed. Assigning values to pseudo variables will result in a syntax error.

Constants

Example:

FOOBAR

Identifiers beginning with an uppercase letter are constants. The definition of a constant (and its initialization) depends on its assignment; constants cannot be defined with a method. Accessing an undefined constant throws a NameError exception.

Within the class or module in which a constant is defined, the constant can be referenced by inheriting classes, classes that include modules, and modules. To reference constants externally, use the '::' operator.

class Foo
  FOO = 'FOO'
end

class Bar < Foo
  p FOO             # => "FOO"
end

p Foo::FOO          # => "FOO"

The names of classes and modules are handled as constants.

Converted from CHM to HTML with chm2web Pro 2.85 (unicode)