Transferred from: http://www.jb51.net/article/48802.htm
Ruby is filled with a series of hidden variables, and we can get some interesting information from these pre-defined global variables.
Global process variables
$$ represents the currently running Ruby process.
>> $$
= 17170
We can kill it ourselves from the current process.
>> ' kill-9 #{$$} '
[1] 17170 killed IRB
$? Represents the state of a recent child process
>> ' echo Hello '
= "Hello\n"
>> $?
= = #<process::status:pid 18048 exit 0>
>> $?. Success?
= True
Exceptions and Errors
$ $ Indicates the information that caused the exception. For example here raise "there's no peanut butter", its value is there ' s no peanut butter.
>> begin raise "This town Ain ' t big enough for the both of us" rescue puts $! End
This town ain ' t big enough for the both of us
= Nil
[Email protected] can give the complete stack call information that caused the error, which is an array.
>> begin raise ' no soup in kitchen ' rescue [email protected] {|trace| puts trace} end
(IRB): 13:in ' irb_binding '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/workspace.rb:80:in ' eval '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/workspace.rb:80:in ' Evaluate '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/context.rb:254:in ' Evaluate '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:159:in ' block (2 levels) in Eval_input '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:273:in ' Signal_status '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:156:in ' block in Eval_input '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in ' block (2 levels) in Each_top_level_ Statement
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in ' Loop '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in ' block in Each_top_level_statement '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in ' Catch '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in ' Each_top_level_statement '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:155:in ' Eval_input '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:70:in ' block in Start '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in ' Catch '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in ' Start '
/home/meck/.rvm/rubies/ruby-1.9.3-p194/bin/>> '
= = ["(>> '"]
Strings and separators
$; Represents the delimiter in the String.Split, the default is a space.
>> "One spaceship, Tiny Tanks, three misplaced Socks". Split
= = ["One Spaceship", "Tiny Tanks", "three misplaced Socks"]
>> $; = ","
= ","
>> "One spaceship, Tiny Tanks, three misplaced Socks". Split
= = ["One Spaceship", "Tiny Tanks", "three misplaced Socks"]
$, used in Array.join and Kernel.print, the default is nil.
>> [' One ', ' I ', ' three ', ' Green '].join
= "Onetwothreegreen"
>> $, = "-"
= "-"
>> [' One ', ' I ', ' three ', ' Green '].join
= "One-two-three-green"
The $/expression reads the line delimiter of the input. It is used in kernel.gets. It usually represents a new row, but can be modified. This is difficult to demonstrate because IRB relies on \ n as the read delimiter, and if the $/is set to nil,gets it will read the entire file.
$\ just the opposite, it is the line delimiter as output.
>> $\ = "Mooooooo"
= "Mooooooo"
>> puts a
nameerror-:-undefined local variable or method ' a ' for main:object-
Mooooooo from (IRB): 25
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/bin/>> '-
Mooooooo >> A
File
Suppose there is a file called Letter.text:
Dear Caroline,
I think we need some honey for tea.
I also think that I could have misplaced my red tie, and you seen it?
-nick
$. Represents the line number that the file is currently being read.
>> open (' letter.txt '). Each {|line| puts "#{$.}: #{line}"}
1:dear Caroline,
2:i think we need some honey for tea.
3:i also think that I could have misplaced my red tie, are you seen it?
4:
5:-nick
= #<file:letter.txt>
The $_ represents the last fetched row.
>> open (' letter.txt '). Each {|line| puts $_.nil?}
True
True
True
True
True
= #<file:letter.txt>
Matching and regular expressions
$~ represents the information that was last matched to, and if so, it returns an example of Matchdata, or nil.
>> "The robots is coming, the robots is coming, the robots is Coming" =~/ro/
= 4
>> $~
= = #<matchdata "Ro" >
>> $~.to_s
= "Ro"
>> "The robots is coming, the robots is coming, the robots is Coming" =~/cats/
= Nil
>> $~
$& is very similar to $~, which returns the string that was last matched.
>> "The robots is coming, the robots is coming, the robots is Coming" =~/ro/
= 4
>> $&
$ ' indicates that the match does not divide the following string.
Copy CodeThe code is as follows:
>> "There were once ten tin robots standing in a row." =~/robot/
= 24
>> $ '
= = "s standing in a row."
= "Ro"
= Nil
Other
$> represents Ruby's default output object, which is used in Kernel.print.
>> $> = $> = $stderr
= #<io:<stderr>>
>> puts ' no no no '
No no No
= Nil
>> $> = $stdin
/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:168:in ' write ': not opened for writing (IOError)
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:168:in ' print '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:168:in ' block (2 levels) in Eval_input '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:273:in ' Signal_status '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:156:in ' block in Eval_input '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:243:in ' block (2 levels) in Each_top_ Level_statement '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in ' Loop '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:229:in ' block in Each_top_level_ Statement
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in ' Catch '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb/ruby-lex.rb:228:in ' Each_top_level_statement '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:155:in ' Eval_input '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:70:in ' block in Start '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in ' Catch '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/irb.rb:69:in ' Start '
From/home/meck/.rvm/rubies/ruby-1.9.3-p194/bin/irb:12:in ' <main> '
$* is probably the most commonly used global variable, which represents an array of all the variables passed to the Ruby file, assuming there is a file called argument_echoer.rb:
$*.each {|arg| puts arg}
Run it:
$ ruby argument_echoer.rb who if Where and why
W.H.O.
What
When
Where
and
Why
Reproduced Introduction to global variables, internal variables, hidden variables in Ruby starting with $