DEV Community

Cover image for Bust Bugs with the Byebug Bug Debugger
Jonathan-Bryant19
Jonathan-Bryant19

Posted on

Bust Bugs with the Byebug Bug Debugger

Byebug is a helpful and easy-to-use debugger made for Ruby. It allows developers to use traditional debugging features to help identify what is happening in a program while it's running. Byebug was created by David Rodriguez in hopes of building a better Ruby debugger.

The core features of Byebug include stepping through a program, setting breakpoints to pause a program, basic REPL functionality, and variable tracking.

Starting Byebug

There are several ways to start a debugging session with Byebug. If you want to start the debugger from the first line of code, you can run your file from the command line and add the "byebug" prefix: $ byebug /path/to/ruby_file.rb. Alternatively, adding the keyword byebug to the source code will trigger a breakpoint and start a byebug session on execution:

def printer(n)

    # breakpoint will trigger here
    byebug

    n.times do |i|
        puts "This is round #{i}"
    end
    puts "hello"
    puts "world"

end

printer(10)
Enter fullscreen mode Exit fullscreen mode

Stopping Byebug

Getting out of a byebug session is just as easy as getting into one. You can terminate a byebug session at any time by using the quit or q command. However, developers should be mindful that code following the current breakpoint will not execute once the byebug session is terminated using quit.

Navigating a Byebug Session

help

It's always great to have quick access to info on what commands you can run while in a byebug session. The help command will return a list of useable commands. help <command-name> will return information specific to that command.
byebug help output

continue

The continue command allows the program to continue running after hitting a breakpoint. The program will continue to run until finishing or hitting another breakpoint. Additionally, when an integer is passed as an argument (e.g., continue 5), the program will execute the number of lines passed as an argument before initiating another breakpoint. Alternatively, the continue! variation will run the program and ignore any future breakpoints.

next

The next command will execute the next line of code and only the next line of code. However, the next command will step over method calls. It will also accept an integer as an argument and will execute the number of lines passed as the argument.

step

step is similar to next with the key difference being that step will step into (and execute) method calls. Since byebug remembers the last command you entered, simply hitting return/enter will continue running the program one line at a time with each press. Combining this feature with byebug's REPL functionality allows developers to access information about a program's state throughout each step of the program's execution.

In this example, entering next would cause byebug to skip the loop and jump to line 8. Entering step will cause the first line of the loop to run. Subsequent step commands will continue executing the loop. While inside the loop, we can access the value of i at any time using byebug's REPL functionality.

next step example

restart

As the name implies, restart will terminate and restart the program in byebug. This could save a few steps as you go through rounds of debugging.

irb

It was a surprise to find that you can enter an IRB or VIM session during a byebug session. Simple type irb to initiate the session. When you exit IRB, you'll be right where you left off in the byebug session.

These commands only begin to scratch the surface of what you can do with byebug. I'd recommend the following resources if you want to take a deeper dive on how to use this fantastic Ruby debugging tool.

Resources

Latest comments (0)