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>'
Öffentliche Instanzmethoden
Source
static VALUE
location_absolute_path_m(VALUE self)
{
return location_realpath(location_ptr(self));
}
Gibt den vollständigen Dateipfad dieses Frames zurück.
Dasselbe wie path, mit dem Unterschied, dass ein absoluter Pfad zurückgegeben wird, auch wenn sich der Frame im Hauptskript befindet.
Source
static VALUE
location_base_label_m(VALUE self)
{
return location_base_label(location_ptr(self));
}
Gibt das Basis-Label dieses Frames zurück, das normalerweise gleich dem Label ist, ohne Dekoration.
Betrachten Sie das folgende Beispiel
def foo puts caller_locations(0).first.base_label 1.times do puts caller_locations(0).first.base_label 1.times do puts caller_locations(0).first.base_label end end end
Das Ergebnis des Aufrufs von foo ist dieses
foo foo foo
Source
static VALUE
location_inspect_m(VALUE self)
{
return rb_str_inspect(location_to_str(location_ptr(self)));
}
Gibt dasselbe zurück wie der Aufruf von inspect auf der String-Repräsentation von to_str
Source
static VALUE
location_label_m(VALUE self)
{
return location_label(location_ptr(self));
}
Gibt das Label dieses Frames zurück.
Besteht normalerweise aus Methodennamen, Klassennamen, Modulnamen usw. mit Dekoration.
Betrachten Sie das folgende Beispiel
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
Das Ergebnis des Aufrufs von foo ist dieses
foo block in foo block (2 levels) in foo
Source
static VALUE
location_lineno_m(VALUE self)
{
return INT2FIX(location_lineno(location_ptr(self)));
}
Gibt die Zeilennummer dieses Frames zurück.
Zum Beispiel mit caller_locations.rb aus Thread::Backtrace::Location
loc = c(0..1).first loc.lineno #=> 2
Source
static VALUE
location_path_m(VALUE self)
{
const rb_iseq_t *iseq = location_iseq(location_ptr(self));
return iseq ? rb_iseq_path(iseq) : Qnil;
}
Gibt den Dateinamen dieses Frames zurück. Dies ist im Allgemeinen ein absoluter Pfad, es sei denn, der Frame befindet sich im Hauptskript, in diesem Fall ist es der Pfad des auf der Befehlszeile übergebenen Skripts.
Zum Beispiel mit caller_locations.rb aus Thread::Backtrace::Location
loc = c(0..1).first loc.path #=> caller_locations.rb
Source
static VALUE
location_to_str_m(VALUE self)
{
return location_to_str(location_ptr(self));
}
Gibt einen String im Stil von Kernel#caller zurück, der diesen Frame repräsentiert.