Debugging Rails applications with Pry or Byebug

Techie     March 2023

Introduction

Debugging is an essential part of developing a Ruby on Rails application. It involves finding and fixing errors or bugs in the code. There are different techniques and tools for debugging Rails applications, but in this tutorial, we’ll focus on two popular Ruby debugging tools: Pry and Byebug.


What is Pry?

Pry is a powerful Ruby REPL (Read-Eval-Print Loop) that allows us to interact with our code in real-time. We can use Pry to debug our Ruby code, inspect objects, and test code snippets. Pry provides several advanced features, including syntax highlighting, code introspection, and method discovery.


What is Byebug?

Byebug is a fast and efficient Ruby debugger that allows us to debug our code interactively. Byebug provides a range of powerful features, including step-by-step execution, breakpoint management, and stack trace inspection.


Installation

Before we can start using Pry or Byebug, we need to install them. To install them, add the following lines to your Gemfile:


gem 'pry-rails'
gem 'byebug'


Using Pry

Once Pry is installed, we can start using it to debug our Rails application. To use Pry, we need to add the following line to the code where we want to start debugging:


binding.pry


This line will pause the execution of the code and open a Pry console, where we can inspect variables, call methods, and execute arbitrary Ruby code.

For example, let’s say we have a controller action that’s not behaving as expected:


def index
  @posts = Post.all
  @categories = Category.all
end


To debug this action with Pry, we can add the binding.pry line after the first line:

def index
  binding.pry
  @posts = Post.all
  @categories = Category.all
end


When we load the page that triggers this action, the execution will pause at the binding.pry line, and we’ll be dropped into a Pry console where we can inspect the @posts and @categories variables, call methods on them, and execute arbitrary Ruby code to help us debug the issue.


Pry Commands

Pry provides several commands that we can use to interact with our code during debugging. Some of the most commonly used commands are:


Using Byebug

Byebug works similarly to Pry, but instead of adding a binding.pry line to the code, we add a byebug line:


def index
  byebug
  @posts = Post.all
  @categories = Category.all
end


When we load the page that triggers this action, the execution will pause at the byebug line, and we’ll be dropped into a Byebug console where we can inspect variables, call methods, and execute arbitrary Ruby code to help us debug the issue.

Byebug also includes a number of commands that can help us navigate the code and inspect variables. Some of the most commonly used commands are:


Additionally, there are a few tips and best practices to keep in mind when debugging with Pry or Byebug:


Conclusion

Debugging Rails applications can be a challenging and time-consuming task, but with the help of tools like Pry and Byebug, we can make the process easier and more efficient. By adding binding.pry or byebug lines to our code, we can pause the execution of the code and inspect variables, call methods, and execute arbitrary Ruby code to help us diagnose and fix issues.


Thanks for reading, see you in the next one!