Episodes

Friday Sep 14, 2007
Ruby special variables
Friday Sep 14, 2007
Friday Sep 14, 2007
There is a collection of special variables whose names consist of a dollar sign ($
) followed by a single character. For example, $$
contains the process id of the ruby interpreter, and is read-only.
$ |
global variable |
@ |
instance variable |
[a-z] or _ |
local variable |
[A-Z] |
constant |
$! |
latest error message |
$@ |
location of error |
$_ |
string last read by gets |
$. |
line number last read by interpreter |
$& |
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] ) |
$= |
case-insensitivity flag |
$/ |
input record separator |
$\ |
output record separator |
$0 |
the name of the ruby script file |
$* |
the command line arguments |
$$ |
interpreter's process ID |
$? |
exit status of last executed child process |
$_
and $~
have local scope. Their names suggest they should be global, but they are much more useful this way, and there are historical reasons for using these names.Version: 20241125
Comments (0)
To leave or reply to comments, please download free Podbean or
No Comments
To leave or reply to comments,
please download free Podbean App.