我们知道,在Ruby语言中存在一些内置变量,这些变量实现的功能不尽相同。下面就让我们一起来看看有关Ruby线程局部域的一些介绍。
Ruby线程局部域变量之$!
最近发生的异常的信息.由raise设定.
- def exception
- begin
- raise "exception test."
- ensure
- puts $!
- end
- end
- exception
结果:
simple.rb:58:in `exception': exception test. (RuntimeError) from simple.rb:64 exception test. # $!中的值
Ruby线程局部域变量之$@
以数组形式保存着发生异常时的back trace信息. 数组元素是字符串,它显示了方法调用的位置,其形式为
"filename:line"或 "filename:line:in `methodname'" 。在向$@赋值时,$!不能为nil。
def exception
begin
raise "exception test."
ensure
puts $@
puts "$@ size is:#{[email protected]}"
end
end
exception