1. Thread::
  2. Backtrace::
  3. Location

class Thread::Backtrace::Location

An object representation of a stack frame, initialized byKernel#caller_locations.

For example:

# caller_locations.rbdefa(skip)caller_locations(skip)enddefb(skip)a(skip)enddefc(skip)b(skip)endc(0..2).mapdo|call|putscall.to_send

Runningruby caller_locations.rb will produce:

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

Here’s another example with a slightly different result:

# foo.rbclassFooattr_accessor:locationsdefinitialize(skip)@locations =caller_locations(skip)endendFoo.new(0..2).locations.mapdo|call|putscall.to_send

Now runruby foo.rb and you should see:

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

Public Instance Methods

Source
static VALUElocation_absolute_path_m(VALUE self){    return location_realpath(location_ptr(self));}

Returns the full file path of this frame.

Same aspath, except that it will return absolute path even if the frame is in the main script.

Source
static VALUElocation_base_label_m(VALUE self){    return location_base_label(location_ptr(self));}

Returns the base label of this frame, which is usually equal to the label, without decoration.

Consider the following example:

deffooputscaller_locations(0).first.base_label1.timesdoputscaller_locations(0).first.base_label1.timesdoputscaller_locations(0).first.base_labelendendend

The result of callingfoo is this:

foofoofoo
Source
static VALUElocation_inspect_m(VALUE self){    return rb_str_inspect(location_to_str(location_ptr(self)));}

Returns the same as callinginspect on the string representation of to_str

Source
static VALUElocation_label_m(VALUE self){    return location_label(location_ptr(self));}

Returns the label of this frame.

Usually consists of method, class, module, etc names with decoration.

Consider the following example:

deffooputscaller_locations(0).first.label1.timesdoputscaller_locations(0).first.label1.timesdoputscaller_locations(0).first.labelendendend

The result of callingfoo is this:

fooblock in fooblock (2 levels) in foo
Source
static VALUElocation_lineno_m(VALUE self){    return INT2FIX(location_lineno(location_ptr(self)));}

Returns the line number of this frame.

For example, usingcaller_locations.rb fromThread::Backtrace::Location

loc =c(0..1).firstloc.lineno#=> 2
Source
static VALUElocation_path_m(VALUE self){    const rb_iseq_t *iseq = location_iseq(location_ptr(self));    return iseq ? rb_iseq_path(iseq) : Qnil;}

Returns the file name of this frame. This will generally be an absolute path, unless the frame is in the main script, in which case it will be the script location passed on the command line.

For example, usingcaller_locations.rb fromThread::Backtrace::Location

loc =c(0..1).firstloc.path#=> caller_locations.rb
Source
static VALUElocation_to_str_m(VALUE self){    return location_to_str(location_ptr(self));}

Returns aKernel#caller style string representing this frame.