class Thread::Backtrace::Location

Eine objektorientierte Darstellung eines Stack-Frames, initialisiert durch Kernel#caller_locations.

Zum Beispiel

# 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

Das Ausführen von ruby caller_locations.rb liefert

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

Hier ist ein weiteres Beispiel mit einem leicht anderen Ergebnis

# 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

Führen Sie nun ruby foo.rb aus, und Sie sollten Folgendes sehen

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