Ruby names are used to refer to constants, variables, methods,
classes, and modules. The first character of a name helps Ruby to
distinguish its intended use. Certain names, listed in Table
18.3 on page 210, are reserved words and should not be used as
variable, method, class, or module names.
Reserved words
|
__FILE__ |
and |
def |
end |
in |
or |
self |
unless |
__LINE__ |
begin |
defined? |
ensure |
module |
redo |
super |
until |
BEGIN |
break |
do |
false |
next |
rescue |
then |
when |
END |
case |
else |
for |
nil |
retry |
true |
while |
alias |
class |
elsif |
if |
not |
return |
undef |
yield |
|
|
In these descriptions,
lowercase letter means the characters
``a'' though ``z'', as well as ``_'', the underscore.
Uppercase
letter means ``A'' though ``Z,'' and
digit means ``0''
through ``9.''
Name characters means any combination of upper-
and lowercase letters and digits.
A local variable name consists of a lowercase letter followed by name
characters.
fred anObject _x three_two_one
|
An instance variable name starts with an ``at'' sign (``
@
'') followed by an
upper- or lowercase letter, optionally followed by name
characters.
A class variable name starts with two ``at'' signs (``
@@
'')
followed by an upper- or lowercase letter, optionally followed by name
characters.
A constant name starts with an uppercase letter followed by name characters.
Class names and module names are constants, and follow the constant
naming conventions. By convention, constant variables are normally
spelled using uppercase letters and underscores throughout.
module Math
PI = 3.1415926
end
class BigBlob
|
Global variables, and some special system variables, start with a
dollar sign (``
$
'') followed by name characters. In addition,
there is a set of two-character variable names in which the second
character is a punctuation character. These predefined variables are
listed starting on page 213. Finally, a global variable name
can be formed using ``
$-
'' followed by
any single character.
$params $PROGRAM $! $_ $-a $-.
|
Method names are described in the section beginning on page 225.