Ruby puts debugging
Aug 2, 2017
Aaron Patterson calls himself a “puts debuggerer”. His article lists useful techniques for debugging ruby code using puts:
- I know where I am but not how I got here:
puts callerwill print the call stack. - I’m calling a method, but I don’t know where it goes:
p method(:render).source_locationwill show you where the methodrenderis defined. - I’m calling super but I don’t know where that goes:
p method(:render).super_method.source_locationwill show you where methodrender’s super is defined. - An object is being mutated, but I don’t know where:
#freezethe object to trigger an exception (with backtrace) when it gets mutated. - I have a deadlock, but I don’t know where: Inside the thread call
p t; puts t.backtraceto get insight.
And then there is always the byebug gem with next, step, etc. when puts is not powerful enough.
