[轉載]Ruby 中$開頭的全域變數、內部變數、隱藏變數介紹

來源:互聯網
上載者:User

標籤:

轉自:http://www.jb51.net/article/48802.htm

Ruby 中充滿了一系列的隱藏變數,我們可以從這些預定義的全域變數中擷取一些有意思的資訊。

全域進程變數

$$ 表示當前啟動並執行 ruby 進程。

 >> $$
=> 17170
我們可以從當前進程殺死它自己
>> `kill -9 #{$$}`
[1]    17170 killed     irb
$? 表示最近一個子進程的狀態
>> `echo hello`
=> "hello\n"
>> $?
=> #<Process::Status: pid 18048 exit 0>
>> $?.success?
=> true

 

異常和錯誤

$1 表示引起異常的資訊。比如在這裡 raise "there‘s no peanut butter",它的值就是 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] 可以給出完整的引起錯誤的棧調用資訊,它是一個數組。
>> 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/>>‘
=> ["(>>‘"]

 

字串和分隔字元

$; 表示 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>

 

匹配和Regex

$~ 表示最近一次正則匹配到的資訊,如果有的話它就返回 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:<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>‘
$* 可能是最常用全域變數,它表示包含傳給 ruby 檔案的所有變數的數組,假設有個叫 argument_echoer.rb 檔案:
$*.each { |arg| puts arg }
運行它:
$ ruby argument_echoer.rb Who What When Where and Why
Who
What
When
Where
and
Why

[轉載]Ruby 中$開頭的全域變數、內部變數、隱藏變數介紹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.