Byebug

Brian Sahota
4 min readNov 12, 2020

--

Me when i finally figure out a problem

After Prying my way out of Mod1 at the Flatiron School I was introduced to the Byebug debugger from one of my teachers in Mod2. During a lecture he said, “lets throw a byebug into one of our controllers and see what’s going on!”

I heard, “I have bedbugs in here what’s going on?”

I did some research and I found Byebug is a very powerful debugger for Rails. It allows you to go inside of your ruby program while it is running. Many of the methods we use are inherited from other classes of code. Our models inherit from ActiveRecord::Base, our migrations inherit from ActiveRecord::Migration, and our controllers inherit from ActionController::Base. It’s a lot, and a good way to find out exactly how the computer processes each line of code is through debugging with Byebug. It gives us many options to step through code, use breakpoints, and has other useful commands.

Byebug is built into Rails, but if you’re an alien and you’re not using Rails you’ll have to run gem install byebug and add require 'byebug' to your code before you can use it.We can then drop a ‘byebug’ anywhere you would like to test as long as you know the gem is in the Gemfile. (We dont need to require ‘byebug’ on top of the page!)

Now when we try and create a new scientist in our browser, the execution hits our byebug command and we’ll get a debugging prompt in our rails server window. Byebug will automatically list 10 lines of code centered around the current line every time it is stopped. The current line is line 24 and it’s marked with a=>

If we type in “help” we can see a list of commands we can use here.

(byebug) help  break      -- Sets breakpoints in the source code
catch -- Handles exception catchpoints
condition -- Sets conditions on breakpoints
continue -- Runs until program ends, hits a breakpoint or reaches a line
debug -- Spawns a subdebugger
delete -- Deletes breakpoints
disable -- Disables breakpoints or displays
display -- Evaluates expressions every time the debugger stops
down -- Moves to a lower frame in the stack trace
edit -- Edits source files
enable -- Enables breakpoints or displays
finish -- Runs the program until frame returns
frame -- Moves to a frame in the call stack
help -- Helps you using byebug
history -- Shows byebug's history of commands
info -- Shows several informations about the program being debugged
interrupt -- Interrupts the program
irb -- Starts an IRB session
kill -- Sends a signal to the current process
list -- Lists lines of source code
method -- Shows methods of an object, class or module
next -- Runs one or more lines of code
pry -- Starts a Pry session
quit -- Exits byebug
restart -- Restarts the debugged program
save -- Saves current byebug session to a file
set -- Modifies byebug settings
show -- Shows byebug settings
source -- Restores a previously saved byebug session
step -- Steps into blocks or methods one or more times
thread -- Commands to manipulate threads
tracevar -- Enables tracing of a global variable
undisplay -- Stops displaying all or some expressions when program stops
untracevar -- Stops tracing a global variable
up -- Moves to a higher frame in the stack trace
var -- Shows variables and its values
where -- Displays the backtrace

We have a lot of commands here. I’d say the most important ones for now would be our:

next / step: These navigational commands are super handy. Typing the next command will move our byebug to the next line, and run that line(s) of code. If number specified, go forward that number of lines. Typing the step command will bring us to the next line as well but it will actually step us into a block or a method.

break / continue: break will set up breakpoints throughout. We can easily set up breakpoints by typing break 9 and it will put a breakpoint on line 9. continue or c runs until the program ends, hits a breakpoint or reaches a line.

restart: Let’s say we’ve made some changes to our code. We’ve made a changes and now we want byebug to reflect those changes. We don’t have to exit and reenter our byebug session. We just type in restart.

As with any tool you need to play with it for a while for it to be second nature. Debugging can be such a deflating task for coding novices. It really helps to have the right tool in your toolbox. Maybe you like Pry a little more because of it’s pretty highlighted syntax. Or maybe you like the functionality of Byebug and the extra commands it gives you. Whatever it is make sure you have a rubber duck on your desk to cry to. Good luck coders!

--

--