Debugging Ruby on Rails Quickly Beginner advice
suggest changeRaising an exception is easier than scanning print logs, and for many bugs it is faster than opening pry or byebug. Those tools should not be the first step.
—
Debugging Ruby/Rails Quickly:
Fast method: raise and .inspect
The fastest way to debug Ruby (especially Rails) code is to raise an exception along the execution path of your code while calling .inspect on the method or object (e.g. foo):
raise foo.inspect
In the above code, raise triggers an Exception that halts execution of your code, and returns an error message that conveniently contains .inspect information about the object/method (i.e. foo) on the line that you’re trying to debug.
This technique is useful for quickly examining an object or method (e.g. is it nil**?) and for immediately confirming whether a line of code is even getting executed at all within a given context.
Fallback: Use a ruby IRB debugger like byebug or pry
Once you know where execution is going, a debugger such as pry or byebug can inspect objects along that path.
To use the byebug gem for debugging in Rails:
- Add
gem 'byebug'inside the development group in your Gemfile - Run
bundle install - Then to use, insert the phrase
byebuginside the execution path of the code you want examined.
byebug is not a variable. When that line runs, it opens an IRB-style session on the current bindings.
Debuggers are useful for a deeper look, but they take longer than raising an error, so they should not be the first step.
—
General Beginner Advice
When you are trying to debug a problem, good advice is to always: Read The !@#$ing Error Message (RTFM)
That means reading error messages carefully and completely before acting so that you understand what it’s trying to tell you. When you debug, ask the following mental questions, in this order, when reading an error message:
- What class does the error reference? (i.e. do I have the correct object class or is my object
nil**?) - What method does the error reference? (Is there a typo in the method name? Can this class respond to it?)
- Finally, using what I can infer from my last two questions, what lines of code should I investigate? (remember: the last line of code in the stack trace is not necessarily where the problem lies.)
In the stack trace pay particular attention to lines of code that come from your project (e.g. lines starting with app/... if you are using Rails). 99% of the time the problem is with your own code.
To illustrate why interpreting in this order is important…
E.g. a Ruby error message that confuses many beginners:
You execute code that at some point executes as such:
@foo = Foo.new
...
@foo.bar
and you get an error that states:
undefined method 'bar' for nil:NilClass
Beginners see this error and think the problem is that the method bar is undefined. It’s not. In this error the real part that matters is:
for nil:NilClass
That means @foo is nil, not a Foo. Ruby is saying bar is not a method on NilClass.
The wording undefined method 'bar' makes it easy to inspect Foo#bar and miss the receiver. Read the class in the message before the method name.
Summary:
Always carefully read the entire error message before beginning any debugging. That means: Always check the class type of an object in an error message first, then its methods, before you begin sleuthing into any stacktrace or line of code where you think the error may be occurring. Those 5 seconds can save you 5 hours of frustration.
tl;dr: Don’t squint at print logs: raise exceptions instead. Avoid rabbit holes by reading errors carefully before debugging.