Skip to content

Instantly share code, notes, and snippets.

@pmauduit
Forked from alainravet/print_stacktrace.rb
Created May 11, 2012 14:37
Show Gist options
  • Save pmauduit/2660129 to your computer and use it in GitHub Desktop.
Save pmauduit/2660129 to your computer and use it in GitHub Desktop.
How to print the stacktrace in Ruby
# 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