Ruby common internal variables

Source: Internet
Author: User

In RubyProgramYou will 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 simpleCodeWhat 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.

 
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.

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.

 
IRB (main): 012: 0 >$ ~ => #

  
   
IRB (main): 026: 0 >1 1 => "foo" IRB (main): 027: 0 >$ ~ [1] => "foo"

  
$'

In the current scope, the regular expression matches the string before the last successful string. If the final match fails, it is nil, equivalent to Regexp. last_match.pre_match.

 
IRB (main): 016: 0 >$ '=> "" # because the last matched content is foobar, there is no character before the input string, so it is ""
$'

In the current scope, the string following the string that the regular expression matches successfully for the last time. If the final match fails, it is nil, equivalent to Regexp. last_match.post_match.

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.

 
IRB (main): 029: 0 >$ + => "bar"
$1
$2
$3...

In the current scope, in the string part of the last successful match of the regular expression, the string corresponding to the last bracket. 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.

 
IRB (main): 029: 0 >$ + => "bar"
Thread Local Domain

The following variables are fully local variables within a thread, but they are independent of each other among different threads.

$!

The latest exception information, which is set by raise.

Def exception begin raise "exception test." ensure puts $! End end exception

Output result:

 
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 isFilename: LineOrFilename: Line: In 'methodname'. $! It cannot be nil.

 
Def exception begin raise "exception test." ensure puts $ @ puts "$ @ size is :#{$ @. Size}" end exception

Output result:

Simple. RB: 58: In 'exception': exception test. (runtimeerror) from simple. RB: 65 simple. RB: 58: In 'exception' # the value in $ @ is an array. The first element is the number of rows where an error occurs, and the second element is the exception content. The following shows the length of the array: simple. RB: 65 $ @ size: 2.
Full Local Area

This type of variable is accessible throughout the application and is referenced by the same variable.

$/

The input record delimiter. The default value is "\ n ".

 
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 ", input end => "test" IRB (main): 079: 0 >$/= "@" # modify the input to "@" => "@" IRB (main ): 080: 0> gets # The read process is not completed after you press enter until you enter "@" and end 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.

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.

 
IRB (main): 087: 0> ["A", "B", "C"]. join => "ABC" IRB (main): 088: 0 >$, = "," # modify the delimiter to "," => "," IRB (main ): 089: 0> ["A", "B", "C"]. join # changed output result => "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.

 
IRB (main): 090: 0 >$ ;=> nil IRB (main): 091: 0> "ABC ". split # by default, the entire character is treated as an element => ["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"]
$ *

The parameter passed to the ruby script. the alias of the internal constant argv.

$

PID of the currently running Ruby process.

 
IRB (main): 094: 0 >$ $ => 5167
$:
$ Load_path

Contains an array containing the list of search directories used when loading files by load or require.

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 & gt; $ 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 ",". "]
Global constant env

Contains the environment variables of the program. It is a hash object.

IRB (main): 006: 0> Env. each {| K, v | puts "# {k }=>#{ v}"} allusersprofile => C: \ Documents and Settings \ All Users appdata => C: \ Documents 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 and Settings \ xp2008 homedrive => C: #===================================== ===== lower ==================
Data

Input stream of the program line after _ end _ is read. If _ end _ is not displayed in the Code, it is not defined.

Stderr, stdin, stdout

Standard input, output, and error stream

Ruby_platform

Ruby interpreter Platform

 
IRB (main): 008: 0> ruby_platform => "i386-mswin32"
Argf is the same as $ *, and argf is the same as $
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.