On this page
Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class · Ruby 4.1
Thread::Backtrace::Location
< Object
9 instance methods
An object representation of a stack frame, initialized by Kernel#caller_locations.
For example:
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:
Example
# 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 methods 9
Ruby version changes 1
-
4.1
-
Thread::Backtrace::Location#source_rangeis added. It returns aRuby::SourceRangefor the Ruby expression associated with the frame.
-