Skip to content

Instantly share code, notes, and snippets.

@alainravet
Created January 24, 2009 21:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alainravet/51560 to your computer and use it in GitHub Desktop.
Save alainravet/51560 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