Ruby 2.4

Thread::Backtrace::Location

class Thread::Backtrace::Location

Parent:Object

由Kernel#caller_locations初始化的堆栈帧的对象表示形式。

例如:

# caller_locations.rb def a(skip) caller_locations(skip) end def b(skip) a(skip) end def c(skip) b(skip) end c(0..2).map do |call| puts call.to_s end

运行ruby caller_locations.rb会产生:

caller_locations.rb:2:in `a' caller_locations.rb:5:in `b' caller_locations.rb:8:in `c'

这是另一个结果稍有不同的例子:

# foo.rb class Foo attr_accessor :locations def initialize(skip) @locations = caller_locations(skip) end end Foo.new(0..2).locations.map do |call| puts call.to_s end

现在运行ruby foo.rb,你应该看到:

init.rb:4:in `initialize' init.rb:8:in `new' init.rb:8:in `<main>'

Public Instance Methods

absolute_path() Show source

返回此帧的完整文件路径。

与路径相同,但包含绝对路径。

static VALUE location_absolute_path_m(VALUE self) { return location_absolute_path(location_ptr(self) }

base_label() Show source

返回此帧的基本标签。

通常和标签一样,没有装饰。

static VALUE location_base_label_m(VALUE self) { return location_base_label(location_ptr(self) }

inspect() Show source

返回值与调用inspectto_str的字符串表示形式相同

static VALUE location_inspect_m(VALUE self) { return rb_str_inspect(location_to_str(location_ptr(self)) }

label() Show source

返回此帧的标签。

通常由方法,类,模块等名称与装饰组成。

考虑下面的例子:

def foo puts caller_locations(0).first.label 1.times do puts caller_locations(0).first.label 1.times do puts caller_locations(0).first.label end end end

调用的结果foo是这样的:

label: foo label: block in foo label: block (2 levels) in foo

static VALUE location_label_m(VALUE self) { return location_label(location_ptr(self) }

lineno() Show source

返回此帧的行号。

例如,使用caller_locations.rbThread::Backtrace::Location

loc = c(0..1).first loc.lineno #=> 2

static VALUE location_lineno_m(VALUE self) { return INT2FIX(location_lineno(location_ptr(self)) }

path() Show source

返回此帧的文件名。

例如,使用caller_locations.rbThread::Backtrace::Location

loc = c(0..1).first loc.path #=> caller_locations.rb

static VALUE location_path_m(VALUE self) { return location_path(location_ptr(self) }

to_s() Show source

返回表示此帧的Kernel#caller样式字符串。

static VALUE location_to_str_m(VALUE self) { return location_to_str(location_ptr(self) }