Ruby 中充滿了一系列的隱藏變量,咱們能夠從這些預約義的全局變量中獲取一些有意思的信息。正則表達式
全局進程變量數組
$$ 表示當前運行的 ruby 進程。
複製代碼 代碼以下:ruby
$$
=> 17170oop
咱們能夠從當前進程殺死它本身
複製代碼 代碼以下:this
kill -9 #{$$}
[1] 17170 killed irblua
$? 表示最近一個子進程的狀態
複製代碼 代碼以下:spa
echo hello
=> "hello\n"
$?
=> #<Process::Status: pid 18048 exit 0>
$?.success?
=> true命令行
異常和錯誤設計
$1 表示引發異常的信息。好比在這裏 raise "there's no peanut butter",它的值就是 there's no peanut butter。
複製代碼 代碼以下:code
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
$@ 能夠給出完整的引發錯誤的棧調用信息,它是一個數組。
複製代碼 代碼以下:
begin raise 'no soup in kitchen' rescue $@.each { |trace| puts trace } end
(irb):13:inirb_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:inevaluate' /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:inblock (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:inblock 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:inloop' /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:incatch' /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:ineval_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:incatch' /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/>>'
=> ["(>>'"]
字符串和分隔符
$; 表示 String.split 裏的分隔符,默認是空格。
複製代碼 代碼以下:
"One Spaceship, Two Tiny Tanks, Three Misplaced Socks".split
=> ["One Spaceship", " Two Tiny Tanks", " Three Misplaced Socks"]
$; = ","
=> ","
"One Spaceship, Two Tiny Tanks, Three Misplaced Socks".split
=> ["One Spaceship", " Two Tiny Tanks", " Three Misplaced Socks"]
$, 用在 Array.join 和 Kernel.print 裏,默認是 nil。
['one', 'two', 'three', 'green'].join
=> "onetwothreegreen"
$, = "-"
=> "-"
['one', 'two', 'three', 'green'].join
=> "one-two-three-green"
$/ 表述讀取輸入的行分隔符。它被用在 Kernel.gets 裏。它一般表示新行,但能夠被修改。這個很難展現,由於 irb 依賴 \n 做爲讀取分隔符,若是把 $/ 設置成 nil,gets 就會讀取整個文件。
$ 正好相反,它是做爲輸出的行分隔符。
複製代碼 代碼以下:
$ = "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
文件
假設有個叫letter.text的文件:
複製代碼 代碼以下:
Dear Caroline,
I think we need some honey for tea.
I also think that I may have misplaced my red tie, have you seen it?
-Nick
$. 表示文件當前被讀取的行號。
複製代碼 代碼以下:
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 may have misplaced my red tie, have you seen it?
4:
5: -Nick
=> #File:letter.txt
$_ 表示最後讀取的行。
複製代碼 代碼以下:
open('letter.txt').each { |line| puts $_.nil? }
true
true
true
true
true
=> #File:letter.txt
匹配和正則表達式
$~ 表示最近一次正則匹配到的信息,若是有的話它就返回 MatchData 的示例,不然就是 nil。
複製代碼 代碼以下:
"the robots are coming, the robots are coming, the robots are coming" =~ /ro/
=> 4
$~
=> #<MatchData "ro">
$~.to_s
=> "ro"
"the robots are coming, the robots are coming, the robots are coming" =~ /cats/
=> nil
$~
$& 跟 $~ 很是類似,它返回最近一次匹配到的字符串。
複製代碼 代碼以下:
"the robots are coming, the robots are coming, the robots are coming" =~ /ro/
=> 4
$&
$' 表示匹配不分後面的字符串。
複製代碼 代碼以下:
"There were once ten tin robots standing in a row." =~ /robot/
=> 24
$'
=> "s standing in a row."
=> "ro"
=> nil
其餘
$> 表示ruby 默認的輸出對象,用在 Kernel.print 裏。
複製代碼 代碼以下:
$> = $> = $stderr
=> #<IO:>
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:inwrite': 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:inblock (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:inblock 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:inloop' 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:incatch' 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:ineval_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:incatch' 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 `'
$* 多是最經常使用全局變量,它表示包含傳給 ruby 文件的全部變量的數組,假設有個叫 argument_echoer.rb 文件:
複製代碼 代碼以下:
$*.each { |arg| puts arg }
運行它:
複製代碼 代碼以下:
$ ruby argument_echoer.rb Who What When Where and Why
Who
What
When
Where
and
why
全局變量由$開頭.它們能夠在程序的任何位置訪問到.在初始化前,全局變量有一個特殊的值 nil.
ruby> $foo
nil
ruby> $foo = 5
5
ruby> $foo
5
應謹慎使用全局變量.因爲在任何地方均可以被寫所以他們至關危險.濫用全局變量會致使很難隔離臭蟲;同時也視爲程序的設計未經嚴格考慮.當你發現必需要使用全局變量時,記得給它一個不會在其它地方一不當心就用到的描述性名字(像上面那樣叫$foo可能不是一個好想法).
全局變量的好處是其能夠被跟蹤;你能夠作一個當變量值改變時被調用的過程.
ruby> trace_var :$x, proc{print "$x is now ", $x, "\n"}
nil
ruby> $x = 5
$x is now 5
5
當一個全局變量(改變時)做爲一個過程的激發器,咱們也管它叫活動變量(active variable).好比說,它可用於保持GUI顯示的更新.
這裏列出了一些以$打頭並跟單個字符的特殊變量.好比,$$包含了Ruby解釋器的進程id,它是隻讀的.這裏是主要的系統變量以及它們的含義(細節可在Ruby的參考手冊中查到):
$! 最近一次的錯誤信息
$@ 錯誤產生的位置
$_ gets最近讀的字符串
$. 解釋器最近讀的行數(line number)
$& 最近一次與正則表達式匹配的字符串
$~ 做爲子表達式組的最近一次匹配
$n 最近匹配的第n個子表達式(和$~[n]同樣)
$= 是否區別大小寫的標誌
$/ 輸入記錄分隔符
$ 輸出記錄分隔符
$0 Ruby腳本的文件名
$* 命令行參數
$$ 解釋器進程ID
$? 最近一次執行的子進程退出狀態
上面的 $_ 和 $~ 都有做用範圍.它們的名字暗示其爲全局的,但它們通常都是這樣用的,關於它們的命名有歷史上的緣由.