Skip to content

Instantly share code, notes, and snippets.

@iamatypeofwalrus
Last active July 28, 2023 13:07
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iamatypeofwalrus/9237651 to your computer and use it in GitHub Desktop.
Save iamatypeofwalrus/9237651 to your computer and use it in GitHub Desktop.
Print stacktrace without raising and Exception in Ruby and/or Rails

Print a stacktrace in Ruby or Rails without raising an exception

Why

You know what method is being and you want to figure out how it got there. Raising an exception is a bit harsh since all you want is a stack trace

How

puts caller

Seriously. It's that easy. If you're getting too much information you could

puts caller[0..2]

Since caller is just an array of strings.

@dchersey
Copy link

dchersey commented Aug 11, 2016

Amazing. So simple, so hard to find out how to do this by googling!
This is saving my hair follicles (what's left of them) as I am using rubymotion for IOS which sadly lacks an effective debugger.
I've been putting logging all over the place to find out where things are being called repeatedly; all I needed to do was

mp caller

(mp is short for motion-print which is better than puts in rubymotion).

And of course in plain ruby, I would just use byebug and get the full debugging experience.

@alexmiddeleer
Copy link

alexmiddeleer commented Aug 31, 2018

❤️ Luckily this is now the first result in google for ruby put stack.

@mddoor
Copy link

mddoor commented Apr 21, 2021

excellent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment