主頁(yè) > 知識(shí)庫(kù) > Ruby 中$開(kāi)頭的全局變量、內(nèi)部變量、隱藏變量介紹

Ruby 中$開(kāi)頭的全局變量、內(nèi)部變量、隱藏變量介紹

熱門(mén)標(biāo)簽:柯城手機(jī)地圖如何做地圖標(biāo)注 外呼線(xiàn)路從哪里出來(lái)的 征服者企業(yè)地圖標(biāo)注 漯河電銷(xiāo) 巫師3地圖標(biāo)注魔力之所 中牟外呼系統(tǒng)違法嗎 AI電銷(xiāo)機(jī)器人 線(xiàn)路 天津外呼系統(tǒng)怎么收費(fèi) 淮安自動(dòng)外呼系統(tǒng)供應(yīng)商

Ruby 中充滿(mǎn)了一系列的隱藏變量,我們可以從這些預(yù)定義的全局變量中獲取一些有意思的信息。

全局進(jìn)程變量

$$ 表示當(dāng)前運(yùn)行的 ruby 進(jìn)程。

復(fù)制代碼 代碼如下:
>> $$
=> 17170

我們可以從當(dāng)前進(jìn)程殺死它自己
復(fù)制代碼 代碼如下:

>> `kill -9 #{$$}`
[1]    17170 killed     irb

$? 表示最近一個(gè)子進(jìn)程的狀態(tài)
復(fù)制代碼 代碼如下:

>> `echo hello`
=> "hello\n"
>> $?
=> #Process::Status: pid 18048 exit 0>
>> $?.success?
=> true

異常和錯(cuò)誤

$1 表示引起異常的信息。比如在這里 raise "there's no peanut butter",它的值就是 there's no peanut butter。

復(fù)制代碼 代碼如下:

>> 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

$@ 可以給出完整的引起錯(cuò)誤的棧調(diào)用信息,它是一個(gè)數(shù)組。
復(fù)制代碼 代碼如下:

>> begin raise 'no soup in kitchen' rescue $@.each { |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 里的分隔符,默認(rèn)是空格。

復(fù)制代碼 代碼如下:

>> "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 里,默認(rèn)是 nil。

>> ['one', 'two', 'three', 'green'].join
=> "onetwothreegreen"
>> $, = "-"
=> "-"
>> ['one', 'two', 'three', 'green'].join
=> "one-two-three-green"


$/ 表述讀取輸入的行分隔符。它被用在 Kernel.gets 里。它通常表示新行,但可以被修改。這個(gè)很難展示,因?yàn)?irb 依賴(lài) \n 作為讀取分隔符,如果把 $/ 設(shè)置成 nil,gets 就會(huì)讀取整個(gè)文件。

$\ 正好相反,它是作為輸出的行分隔符。

復(fù)制代碼 代碼如下:

>> $\ = "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

文件

假設(shè)有個(gè)叫l(wèi)etter.text的文件:

復(fù)制代碼 代碼如下:

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


$. 表示文件當(dāng)前被讀取的行號(hào)。
復(fù)制代碼 代碼如下:

>> 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>

$_ 表示最后讀取的行。
復(fù)制代碼 代碼如下:

>> open('letter.txt').each { |line| puts $_.nil? }
true
true
true
true
true
=> #File:letter.txt>

匹配和正則表達(dá)式

$~ 表示最近一次正則匹配到的信息,如果有的話(huà)它就返回 MatchData 的示例,否則就是 nil。

復(fù)制代碼 代碼如下:

>> "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
>> $~

$ 跟 $~ 非常相似,它返回最近一次匹配到的字符串。
復(fù)制代碼 代碼如下:

>> "the robots are coming, the robots are coming, the robots are coming" =~ /ro/
=> 4
>> $

$' 表示匹配不分后面的字符串。
復(fù)制代碼 代碼如下:

>> "There were once ten tin robots standing in a row." =~ /robot/
=> 24
>> $'
=> "s standing in a row."
=> "ro"
=> nil

其他

$> 表示ruby 默認(rèn)的輸出對(duì)象,用在 Kernel.print 里。

復(fù)制代碼 代碼如下:

>> $> =  $> = $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 文件的所有變量的數(shù)組,假設(shè)有個(gè)叫 argument_echoer.rb 文件:
復(fù)制代碼 代碼如下:

$*.each { |arg| puts arg }

運(yùn)行它:
復(fù)制代碼 代碼如下:

$ ruby argument_echoer.rb Who What When Where and Why
Who
What
When
Where
and
Why

您可能感興趣的文章:
  • Ruby基本的環(huán)境變量設(shè)置以及常用解釋器命令介紹
  • Ruby中變量引用時(shí)的一些注意點(diǎn)
  • Ruby中類(lèi)變量和實(shí)例變量的比較
  • 詳細(xì)解析Ruby中的變量
  • ruby 局部變量
  • ruby 實(shí)變量
  • 深入分析Ruby 變量

標(biāo)簽:克拉瑪依 河池 大慶 西雙版納 南昌 棗莊 內(nèi)江 甘孜

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ruby 中$開(kāi)頭的全局變量、內(nèi)部變量、隱藏變量介紹》,本文關(guān)鍵詞  Ruby,中,開(kāi)頭,的,全局,變量,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Ruby 中$開(kāi)頭的全局變量、內(nèi)部變量、隱藏變量介紹》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Ruby 中$開(kāi)頭的全局變量、內(nèi)部變量、隱藏變量介紹的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章