Dada
|> Structures
|> Algorithms

Getting Started With Byebug

The bare minimum to start using Byebug

This is a very quick guide/checklist on how to install and get debugging with Byebug, a Ruby debugger.

Installing byebug

gem install byebug

Or add to your Gemfile

gem byebug

Setting a breakpoint and starting a session

To add the point where byebug will start the session in your code, essentially, the breakpoint, you type the method "byebug" in your code. If your code looks like this,

def my_awesome_function()
  process
  log
end

You would set the breakpoint like this

def my_awesome_function()
  byebug
  process
  log
end

If you are running your code directly, the process should stop when your code reaches the line with byebug. If you are using something like foreman, then you have the option to connect to a sockets session on another terminal tab.

The debugger screen

Once byebug starts its session, you will see the debugger screen. This screen will give you the file name, and then some lines of the code. There will be a arrow showing you the current place where the code execution has stopped.

To step over the next line of code, enter "next".

To step into the next line of code, enter "step"

To step out, enter, "finish". This last one doesn't seem to work as I would want it to, so sometimes I have to enter it several times.

If by querying values you can't see the code where you are stopped, you can get the listing back by typing "list =".

To let the program run, type "continue".

Navigating the Stack Trace

To see the stack trace, type "backtrack". This should give you a pretty output of the stack trace.

If you want to go back in the stack trace so that you can query the environment at a previous point in the execution, type "up".

Once you are done doing your exploring, you can come back to later points by typing "down".

Querying current values

Perhaps one of the most important things you can do in the debugging console is to query the value of different variables.

You can examine variables by typing their name and hitting enter.

(byebug) name
"My name"

Trying out solutions

If you think you found a way to solve your bug, you can try the solution directly in the console to see if it works out.

Learning more

The debugger becomes a better tool if your add to it an understanding on Ruby's object model and method dispatch. Having a good command on Ruby's reflection methods also helps.

For more information on Byebug itself and to explore other command, look at its documentation on github https://github.com/deivid-rodriguez/byebug