Ruby--Kernel#caller方法
2016-03-04 10:02
232 查看
用途:
caller可以返回当前位置的调用者详细信息,异常的错误栈信息(Exception#backtrace)即通过该方法获得。
caller([level])
示例:
def foo
p caller(0)
p caller(1)
p caller(2)
p caller(3)
end
def bar
foo
end
bar
=> [“-:2:in
[“-:9:in `bar’”, “-:12”]
[“-:12”]
[]
参考资料:
http://ruby-doc.org/core-2.3.0/Kernel.html#method-i-caller
http://www.kuqin.com/rubycndocument/man/stdlib_function.html
caller可以返回当前位置的调用者详细信息,异常的错误栈信息(Exception#backtrace)即通过该方法获得。
caller([level])
以$@的back trace(字符串数组)形式返回level层上(省略参数时为1)的调用者的信息。若是顶层的话就返回空数组。将caller的返回值代入$@就可以设定异常的发生位置。
示例:
def foo
p caller(0)
p caller(1)
p caller(2)
p caller(3)
end
def bar
foo
end
bar
=> [“-:2:in
foo'", "-:9:inbar’”, “-:12”]
[“-:9:in `bar’”, “-:12”]
[“-:12”]
[]
参考资料:
http://ruby-doc.org/core-2.3.0/Kernel.html#method-i-caller
http://www.kuqin.com/rubycndocument/man/stdlib_function.html
相关文章推荐
- 有趣的Ruby-学习笔记6
- iOS_ruby环境的配置
- 安装 Ruby运行环境----> CocoaPods安装和使用图解
- 每个程序员都应该学习使用Python或Ruby
- 解决国内ruby gem不能用的问题
- ruby on rails 入门(1) 环境安装
- 有趣的Ruby-学习笔记5
- RubyCritic:一款不错的检测代码质量工具
- RubyCritic:一款不错的检测代码质量工具
- ruby安装
- Ruby中unless和if not明明相似,为何不去除一者?
- 10个基于 Ruby on Rails 构建的顶级站点
- 10个基于 Ruby on Rails 构建的顶级站点
- Ruby术语表
- mac上Ruby环境安装教程
- ruby on rails出现的问题ActiveModel::ForbiddenAttributesError
- 如何在Mac中安装好Ruby环境
- MAC搭建RUBY
- ruby on rails爬坑(四):使用AWS S3存取数据
- Ruby 简介