I need to get a stack trace object in Ruby; not to print it, just to get it to do some recording and dumping for later analysis. Is that possible? How?
相关问题
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
- gem cleanup shows error: Unable to uninstall bundl
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Threading in C# , value types and reference types
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- uninitialized constant Mysql2::Client::SECURE_CONN
Try
You can use Kernel.caller for this. The same method is used when generating stack traces for exceptions.
From the docs:
Try error.backtrace:
produces:
You can create your own if you want as well. As demonstrated in Eloquent Ruby by Russ Olsen:
This will give you an array which contains all the lines that you may get in any normal backtrace.
For Ruby 2.0+, you can use
Kernel#caller_locations
. It is essentially the same asKernel#caller
(covered in Sven Koschnicke's answer), except that instead of returning an array of strings, it returns an array ofThread::Backtrace::Location
objects.Thread::Backtrace::Location
provides methods such aspath
,lineno
, andbase_label
, which may be useful when you need access to specific details about the stack trace, and not just a raw string.From the docs:
Usage example: