123456789_123456789_123456789_123456789_123456789_

Class: Thread::Backtrace::Location

Relationships & Source Files
Inherits: Object
Defined in: vm_backtrace.c

Overview

An object representation of a stack frame, initialized by Kernel.caller_locations.

For example:

# 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

Running ruby 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.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

Now run ruby foo.rb and you should see:

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

Instance Method Summary

  • #absolute_path

    Returns the full file path of this frame.

  • #base_label

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

  • #inspect

    Returns the same as calling inspect on the string representation of #to_str

  • #label

    Returns the label of this frame.

  • #lineno

    Returns the line number of this frame.

  • #path

    Returns the file name of this frame.

  • #to_s

    Returns a Kernel.caller style string representing this frame.

Instance Method Details

#absolute_path

Returns the full file path of this frame.

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

[ GitHub ]

  
# File 'vm_backtrace.c', line 422

static VALUE
location_absolute_path_m(VALUE self)
{
    return location_realpath(location_ptr(self));
}

#base_label

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

Consider the following example:

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

The result of calling foo is this:

foo
foo
foo
[ GitHub ]

  
# File 'vm_backtrace.c', line 348

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

#inspect

Returns the same as calling inspect on the string representation of #to_str

[ GitHub ]

  
# File 'vm_backtrace.c', line 487

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

#label

Returns the label of this frame.

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

Consider the following example:

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

The result of calling foo is this:

foo
block in foo
block (2 levels) in foo
[ GitHub ]

  
# File 'vm_backtrace.c', line 308

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

#lineno

Returns the line number of this frame.

For example, using caller_locations.rb from Location

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

  
# File 'vm_backtrace.c', line 196

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

#path

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, using caller_locations.rb from Location

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

  
# File 'vm_backtrace.c', line 370

static VALUE
location_path_m(VALUE self)
{
    const rb_iseq_t *iseq = location_iseq(location_ptr(self));
    return iseq ? rb_iseq_path(iseq) : Qnil;
}

#to_s

Returns a Kernel.caller style string representing this frame.

[ GitHub ]

  
# File 'vm_backtrace.c', line 477

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