Ruby 中$开头的变量介绍
来源:爱站网时间:2019-06-06编辑:网友分享
Ruby中充满了隐藏变量,我们可以从这些预定义的全局变量中获得一些有趣的信息,今天爱站技术频道小编和大家分享Ruby 中$开头的变量介绍,希望对你学习有帮助!
Ruby中充满了隐藏变量,我们可以从这些预定义的全局变量中获得一些有趣的信息,今天爱站技术频道小编和大家分享Ruby 中$开头的变量介绍,希望对你学习有帮助!
全局进程变量
$$ 表示当前运行的 ruby 进程。
复制代码 代码如下:
>> $$
=> 17170
=> 17170
我们可以从当前进程杀死它自己
复制代码 代码如下:
>> `kill -9 #{$$}`
[1] 17170 killed irb
$? 表示最近一个子进程的状态
$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
$@ 可以给出完整的引起错误的栈调用信息,它是一个数组。
$; 表示 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。
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?
复制代码 代码如下:
>> 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>
$_ 表示最后读取的行。
$~ 表示最近一次正则匹配到的信息,如果有的话它就返回 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
>> $&
$' 表示匹配不分后面的字符串。
$> 表示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 中$开头的变量介绍,如果你还想了解更多技术方面的知识,你可以来关注爱站技术频道网,爱站技术频道小编会为你服务。