Ruby variables and constants hold references to objects.
Variables
themselves do not have an intrinsic type. Instead, the type of a
variable is defined solely by the messages to which the object
referenced by the
variable responds.
[When we say that a variable
is not typed, we mean that any given variable can at different times
hold references to objects of many different types.]
A Ruby
constant is also a reference to an object.
Constants are
created when they are first assigned to (normally in a class or module
definition). Ruby, unlike less flexible languages, lets you alter the value
of a constant, although this will generate a warning message.
MY_CONST = 1
MY_CONST = 2 # generates a warning
|
produces:
prog.rb:2: warning: already initialized constant MY_CONST
|
Note that although constants should not be changed, you can alter the
internal states of the objects they reference.
MY_CONST = "Tim"
|
MY_CONST[0] = "J" # alter string referenced by constant
|
MY_CONST
|
� |
"Jim"
|
Assignment potentially
aliases objects,
giving the same object different names.