Debug Rails with byebug

There is a really nice gem for debugging Ruby and Rails application called byebug. The usage differs a bit from IDE integrated debuggers like C# one, but it works perfectly as well.

First of all, you need to add byebug to your Gemfile.

gem 'byebug'

After that, run the bundle install command and after restarting your rails server you are almost good to go.

Last thing you need to do is adding a byebug line to your code.

def show
  @player = Player.find(params[:id])
  byebug
  redirect_to game_path(@player.game) if @player.game.finished?
end

Your code will stop on that line and transfer you to the rails/ruby console. You might want to fiddle around a little, get to know the contents of the actual state, or execute some methods.

Everything happens in the command line, so you will feel like a true hacker and if you become tired of debugging just type continue and the execution resumes.


Would you like to get the most interesting content about programming every Monday?
Sign up to Programming Digest and stay up to date!