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
$@ 可以给出完整的引起错误的栈调用信息,它是一个数组。
复制代码 代码如下:
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 里的分隔符,默认是空格。
复制代码 代码如下:
"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:<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
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]