I’ve been coding in Ruby/Rails for almost 9 months now, having spent years before that in Python.
While I’m really enjoying Rails, there’s one area where I often find myself frustrated: chasing down stubborn bugs. In other languages I can almost always track down difficulties without too much trouble, but when I hit a wall debugging rails, I tend to really hit a wall. I guess what I’m asking is: what strategies do advanced rails users employ to track down more stubborn errors?
At the moment my approach is usually:
-
Examine the stack trace (most simple bugs solved here)
-
Run debugger/pry/console & examine the environment, pace through each step if necessary
-
Google it
-
Post on stack overflow/github issues
-
Procrastinate and/or swear profusely
If any advanced rails-ers would share their strategy for chasing down more stubborn bugs, I’d be really appreciative. In short, what do yo do when trace/debugger don’t offer any clues?
Being with Rails mere 5 years, I do not consider myself an advanced Railser, but nevertheless I’ll happily share my knowledge. 🙂
The main thing when dealing with any (except some very, very trivial ones) is to write a test for this bug.
A few times it happened that I solved the bug at this stage – for example when the bug was related to the automatic class reloading, which is active in development, and turned off in test mode.
Then I usually just place some
logger.debugstatements with a lot ofinspectandcaller(0).join("\n\t")in it. Then I very carefully examine the log file.Because the ‘test.log’ can have a few hundreds megabytes, I always remember to zero it before I run my test. Also I usually run just one test method at the time, because the output would be unreadable otherwise.
I do not use a dedicated debugger. In some old version of ruby the debugger stopped working, I learned to live without it, and never looked back.
A few utilities which may be useful:
A function defined in my
~/.bashrc, which lets me to invoke a single test method (or a group of methods):And this method helps me with logging and checking the timing of some steps: