Created
January 24, 2009 21:12
-
-
Save alainravet/51560 to your computer and use it in GitHub Desktop.
How to print the stacktrace in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to print the stacktrace from anywhere : | |
# 1° | |
#------------------------------------------------------------------------------ | |
module Kernel | |
def print_stacktrace | |
raise | |
rescue | |
puts $!.backtrace[1..-1].join("\n") | |
end | |
end | |
# 2 | |
#------------------------------------------------------------------------------ | |
class J | |
def self.bar | |
puts "before" | |
print_stacktrace #<<------------- | |
puts "after" | |
end | |
end | |
class K | |
def self.foo | |
J.bar | |
end | |
end | |
begin | |
K.foo | |
end | |
#------------------------------------------------------------------------------ | |
__END__ | |
RESULT : | |
before | |
/Users/test/sanbox.rb:19:in `bar' | |
/Users/test/sanbox.rb:26:in `foo' | |
/Users/test/sanbox.rb:31 | |
after | |
Program exited with code 0 after 0.03 seconds. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment