class Thread::Backtrace::Location

堆栈帧的对象表示,由 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>'