Tell Those Bugs “Buhbye” With byebug.

Joseph Patterson
4 min readMar 26, 2020

Published by Joseph Patterson

Face it: Coding can be daunting. There are plenty syntax pitfalls and a whole lot of logic that can get sideways in hurry. What do you do when you can’t figure out that unexpected nil return or pesky strong params paradox? Rails comes to the rescue with a nifty solution in the form of byebug.

Debugging in Rails

Who indeed?

Rails provides several choices when it comes to debugging:

You can use the puts or p command. Using p is the same as saying puts variable.inspect.

This is great when you want to look up an object…

but if you end up with a lot of variables p becomes a hassle.

“Binding.pry” is a step up in that it lets you require a pry within your running code and pauses it in order to see what is going on in an irb-like setting. I won’t stop to jump in the ocean of uses for Binding.pry but have linked to an amazing resource for its many benefits:

https://dev.to/elimerrell/debugging-with-pry-a-beginners-guide-3p99

There might be situation where you need to step it up a notch and really get to debugging your code. Watch out pests, its byebug.

byebug and its benefits

Byebug is a debugger tool that has a leg up over binding.pry. Pry will stop your app where you have added it into your code and doesn’t add any functionality, while byebug works a little different.

byebug in a create method

When the code is run and a new picture is instantiated, the program will pause and in the terminal you can see this.

Within the terminal, there are several functions you can access. Here I’ve called the picture params to return what its values are at this point in the program. I was expecting to have an image_url, title, and user_id return and sure enough, I got them back. This is useful to test line by line and see what is going on behind the scenes. You don’t have to quit running your code either to keep going. Here are some helpful commands byebug offers.

  • n => executes the next line of code. This will not step into a function however. It will just execute the function and move to the next line it.
  • s => steps into the next stack of code so the next function can be ran.
  • c => continues the program until it either finishes hits another breakpoint.
  • l => outputs the source code that is currently being ran.
  • q => quits the byebug and returns back to the program.

With these simple commands you can confidently run through your code and see what is going on behind the scenes. Byebug sessions can even be saved in files to revisit later!

Say Bye to Bugs

With a little practice, byebug can become a powerful tool in your coding arsenal. You don’t have be fluent in it to be a good coder, but it can save you hours finding out exactly what is happening in your program. As a new developer it is helping me to understand where the bugs are in my Rails app projects.

--

--