Global variables start with $.ProgramBefore initialization, the global variable has a special value nil.
Special variables starting with $ and with a single character are listed here. for example, $ contains the process ID of the Ruby interpreter, which is read-only. here are the main system variables and their meanings (details can be found in the ruby reference manual ):
$! Last error message
$ @ Error location
$ _ Gets recently read string
$. Number of lines recently read by the interpreter (line number)
$ & The latest string matching the Regular Expression
$ ~ Last match as a child expression group
$ N the nth subexpression (and $ ~ [N] Same)
$ = Case-insensitive flag
$/Input record Separator
$ \ Output record Separator
$0 Ruby script file name
$ * Command line parameters
$ Interpreter process ID
$? The last sub-process exited
The above $ _ and $ ~ Their names indicate global, but they are generally used in this way. There are historical reasons for their names.
# Getting the current file name
Puts _ file __
# Obtain the Directory Name of the current file
Puts file. dirname (_ file __)
# Obtain the complete name of the current file
# Require 'pathname' is required to obtain the complete path ',CodeAs follows:
Require 'pathname'
Puts pathname. New (_ file _). realpath
# Obtain the complete directory of the current file
Require 'pathname'
Puts pathname. New (file. dirname (_ file _). realpath
In Ruby programs, we often see some variables starting with $. These are not the global variables we set in the program, but the variables that have been set inside the system, they represent some specific meanings. Some common internal variables are collected below, and some simple code is used to explain what they mean:
Local Domain:
It can only be valid within a thread scope. The following can also be seen as a local variable in the thread.
PS: the variables described here are related to regular expression matching. The code for the regular expression matching process is the same. Here, the code for regular expression matching is not repeated, the complete input and output are provided only in the first example, and the subsequent example only outputs the value of the variable directly.
$ _
The last string read by gets or Readline. If EOF is encountered, it is nil. The scope of this variable is a local domain.
Ruby code
IRB (main): 006: 0 >$ _
=> Nil
IRB (main): 007: 0> gets
Foobar
=> "Foobar \ n"
IRB (main): 008: 0 >$ _
=> "Foobar \ n"
$ &
In the current scope, the regular expression matches the string successfully for the last time. If the last match fails, it is nil.
Ruby code
IRB (main): 002: 0 >$ &
=> Nil
IRB (main): 010: 0>/(FOO) (bar) (BAZ )? /= ~ "Foobarbaz"
=> 0
IRB (main): 011: 0 >$ &
=> "Foobar"
$ ~
Information about the last successful match in the current scope (matchdata object-class set to process information related to the matching process of the regular expression .).
You can use $ ~ The form of [N] extracts the nth matching result ($ n) from the data. equivalent to $1, $2...
Equivalent to Regexp. last_match.
Ruby code
IRB (main): 012: 0 >$ ~
==## <Matchdata "foobar" 1: "foo" 2: "bar" 3: Nil>
IRB (main): 026: 0> $1
=> "Foo"
IRB (main): 027: 0 >$ ~ [1]
=> "Foo"
$'
In the current scope, the regular expression matches the string before the last successful match. If the last match fails, it is nil.
Equivalent to Regexp. last_match.pre_match.
Ruby code
IRB (main): 016: 0> $'
=> "" # Because the last matched content is foobar, there are no other characters before the input string, so it is ""
$'
In the current scope, the string next to the string that the regular expression matches successfully for the last time. If the last match fails, it is nil.
Equivalent to Regexp. last_match.post_match.
Ruby code
IRB (main): 028: 0> $'
=> "Baz"
$ +
In the current scope, the string corresponding to the last parenthesis in the string part of the last successful match of the regular expression. if the final match fails, it is nil. in a multiple-choice Matching Model, this variable is useful if you cannot determine which part of the matching is successful.
Ruby code
IRB (main): 029: 0 >$ +
=> "Bar"
$1
$2
$3...
Stores the values matching the nth parentheses when the last model match is successful. if no parentheses exist, the value is nil. equivalent to Regexp. last_match [1], Regexp. last_match [2],...
Ruby code
IRB (main): 030: 0> $1
=> "Foo"
IRB (main): 031: 0> $2
=> "Bar"
IRB (main): 032: 0> $3
=> Nil
Thread Local Domain
The following variables are fully local variables within a thread, but they are independent of each other among different threads.
$!
Information about the most recent exception. It is set by raise.
Ruby code
Def exception
Begin
Raise "exception test ."
Ensure
Puts $!
End
End
Exception
Result:
Reference
Simple. RB: 58: In 'exception': exception test. (runtimeerror)
From simple. RB: 64
Exception test. # $! Value in
$ @
The back trace information when an exception occurs is saved as an array. The array element is a string that shows the location of the method call. Its form is
"Filename: line"
Or
"Filename: Line: In 'methodname '"
$! It cannot be nil.
Ruby code
Def exception
Begin
Raise "exception test ."
Ensure
Puts $ @
Puts "$ @ size is :#{$ @. Size }"
End
End
Exception
Result:
Reference
Simple. RB: 58: In 'exception': exception test. (runtimeerror)
From simple. RB: 65
Simple. RB: 58: In 'exception' # $ @ indicates the value in an array. The first element is the number of rows where an error occurs, and the second element is the exception content. The length of the array is printed below
Simple. RB: 65
$ @ Size: 2
Full Local Area
This type of variable is accessible throughout the application and is referenced by the same variable. Is Global
$/
The input record delimiter. The default value is "\ n ".
Ruby code
IRB (main): 076: 0 >$/# initial input delimiter
=> "\ N"
IRB (main): 077: 0> gets
=> "\ N"
IRB (main): 078: 0> "test" # After you press enter, "\ n" is inserted by default. The input ends.
=> "Test"
IRB (main): 079: 0 >$/= "@" # change the input "@"
=> "@"
IRB (main): 080: 0> gets # The read process is not completed after you press enter until you enter "@".
Test
@
=> "Test \ n @"
$ \
Output record separator. Print will output the string at the end.
The default value is nil. No characters are output at this time.
Ruby code
IRB (main): 082: 0> Print "ABC"
ABC => Nil
IRB (main): 083: 0 >$ \= "@"
=> "@"
IRB (main): 084: 0> Print "ABC"
ABC @ => Nil
$,
Default delimiter. If the parameter is omitted in array # Join or is output between print parameters.
The default value is nil, which is equivalent to a null string.
Ruby code
IRB (main): 087: 0> ["A", "B", "C"]. Join
=> "ABC"
IRB (main): 088: 0 >$, = "," # modify the delimiter ","
=> ","
IRB (main): 089: 0> ["A", "B", "C"]. Join # The output result is changed.
=> "A, B, C"
IRB (main): 090: 0>
$;
The splitting character when the parameter is omitted in string # split. The default value is nil. In this case, a special split is made.
Ruby code
IRB (main): 090: 0 >$;
=> Nil
IRB (main): 091: 0> "ABC". Split # The entire character is treated as an element by default when it is split.
=> ["ABC"]
IRB (main): 092: 0 >$; = "B"
=> "B"
IRB (main): 093: 0> "ABC". Split # When "B" is used as the delimiter, the entire character is treated as two elements.
=> ["A", "C"]
$ *
Parameter passed to the ruby script. Alias of the internal constant argv
$
PID of the currently running Ruby process.
Ruby code
IRB (main): 094: 0 >$ $
=> 5167
$:
$ Load_path
Contains an array containing the list of search directories used when loading files by load or require.
Ruby code
IRB (main): 095: 0 >$:
=> ["/Usr/local/lib/site_ruby/1.8", "/usr/local/lib/site_ruby/1.8/i486-linux ", "/usr/local/lib/site_ruby/1.8/i386-linux", "/usr/local/lib/site_ruby", "/usr/lib/Ruby/vendor_ruby/1.8 ", "/usr/lib/Ruby/vendor_ruby/1.8/i486-linux", "/usr/lib/Ruby/vendor_ruby", "/usr/lib/Ruby/1.8 ", "/usr/lib/Ruby/1.8/i486-linux", "/usr/lib/Ruby/1.8/i386-linux ",". "]
IRB (main): 096: 0> $ load_path
=> ["/Usr/local/lib/site_ruby/1.8", "/usr/local/lib/site_ruby/1.8/i486-linux ", "/usr/local/lib/site_ruby/1.8/i386-linux", "/usr/local/lib/site_ruby", "/usr/lib/Ruby/vendor_ruby/1.8 ", "/usr/lib/Ruby/vendor_ruby/1.8/i486-linux", "/usr/lib/Ruby/vendor_ruby", "/usr/lib/Ruby/1.8 ", "/usr/lib/Ruby/1.8/i486-linux", "/usr/lib/Ruby/1.8/i386-linux ",". "]
There are also some global constants:
1. env, which contains the environment variable of the program, is a hash object.
Ruby code
IRB (main): 006: 0> env. Each {| K, v | puts "# {k }=>#{ v }"}
Allusersprofile => C: \ Documents ents and Settings \ All Users
Appdata => C: \ Documents ents and Settings \ xp2008 \ Application Data
Classpath =>.; C: \ Program Files \ ringZ studio \ storm codec \ qtsystem \ qtjava.zip
Clientname => Console
Commonprogramfiles => C: \ Program Files \ common files
Computername => hooopo
Comspec => c: \ windows \ system32 \ cmd.exe
Fp_no_host_check => NO
Home => C: \ Documents ents and Settings \ xp2008
Homedrive => C:
Homepath => \ Documents ents and Settings \ xp2008
Include => C: \ Program Files \ Microsoft Visual Studio \ vc98 \ ATL \ include; C: \ Program F
Les \ Microsoft Visual Studio \ vc98 \ MFC \ include; C: \ Program Files \ Microsoft Visual
Tudio \ vc98 \ include
Inputrc => C: \ Ruby \ bin \ inputrc. Euro
Java_home => C: \ Program Files \ Java \ jdk1.7.0 \
Lang => zh_cn
Lib => C: \ Program Files \ Microsoft Visual Studio \ vc98 \ MFC \ Lib; C: \ Program Files \ mic
Osoft Visual Studio \ vc98 \ Lib
Logonserver =>\\ hooopo
Msdevdir => C: \ Program Files \ Microsoft Visual Studio \ common \ msdev98
Number_of_processors => 1
OS => windows_nt
Path => C: \ Program Files \ PC Connectivity Solution \; C: \ Perl \ site \ bin; C: \ Perl \ bin; c
\ Ruby \ bin; C: \ TCL \ bin; C: \ Program Files \ imagemagick-6.4.1-q8; c: \ windows \ system32;
: \ Windows; c: \ windows \ system32 \ WBEM; C: \ Program Files \ ringZ studio \ storm codec \ QT
Ystem \; C: \ Program Files \ Microsoft SQL Server \ 80 \ tools \ binn; C: \ Program Files \ mic
Osoft SQL Server \ 90 \ tools \ binn \; C: \ Program Files \ Java \ jdk1.7.0 \; C: \ Program File
\ E \ cmd; C: \ Program Files \ git \ bin; C: \ Program Files \ Java \
Dk1.7.0 \; C: \ Program Files \ Microsoft Visual Studio \ common \ tools \ WINNT; C: \ Program
Files \ Microsoft Visual Studio \ common \ msdev98 \ bin; C: \ Program Files \ microsoft vis
Al studio \ common \ tools; C: \ Program Files \ Microsoft Visual Studio \ vc98 \ bin; C: \ sho
S \ 0. r1134 \ ..; C: \ python25 \; C: \ shoes3 \ 0. r1243 \..
Pathext =>. com;. EXE;. BAT;. CMD;. vbs;. VBE;. js;. JSE;. WSF;. wsh;. TCL
Processor_architecture => x86
Processor_identifier => x86 family 15 model 4 stepping 9, genuineintel
Processor_level => 15
Processor_revision = & gt; 0409
ProgramFiles => C: \ Program Files
Prompt => $ p $ G
Qtjava => C: \ Program Files \ ringZ studio \ storm codec \ qtsystem \ qtjava.zip
Rubyopt =>-rubygems
Sessionname => Console
Sst_fastmem => 1
Sst_swap_en_wait_on_vsync => 0
Systemdrive => C:
Systemroot => C: \ WINDOWS
Temp => C: \ Alibaba E ~ 1 \ xp2008 \ locals ~ 1 \ Temp
TMP => C: \ release E ~ 1 \ xp2008 \ locals ~ 1 \ Temp
Userdomain => hooopo
Username => xp2008
USERPROFILE => C: \ Documents ents and Settings \ xp2008
Vs80comntools => C: \ Program Files \ Microsoft Visual Studio 8 \ common7 \ tools \
WINDIR => C: \ WINDOWS
2. Data: Read the input stream of the program line after _ end. If _ end _ is not displayed in the Code, it is not defined.
3. stderr, stdin, stdout
Standard input, output, and error stream
4. ruby_platform, Ruby interpreter Platform
Ruby code
IRB (main): 008: 0> ruby_platform
=> "I386-mswin32"
5. argf is the same as $ *, and argf is the same as $ <
There are still many that are not recommended for use.