Ruby on Rails / Testing and Debugging Rails Applications

Using Pry and Byebug for Debugging

This tutorial will take you through using Pry and Byebug, two powerful debugging tools for Ruby. You will learn how to pause your code, inspect variables, and follow the execution…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Teaches unit testing, integration testing, and debugging techniques in Rails.

1. Introduction

Welcome to this tutorial on using Pry and Byebug for debugging in Ruby.

Our goal in this tutorial is to get you comfortable with using Pry and Byebug, two powerful debugging tools that allow you to pause your code execution, inspect variables and follow the flow of your code.

By the end of this tutorial, you will have a solid understanding of how to set breakpoints, step through your code, inspect variables and navigate code execution.

Prerequisites
Before we get started, make sure you have Ruby installed on your system. Some familiarity with Ruby syntax would be helpful.

2. Step-by-Step Guide

Installation

First, we need to install Pry and Byebug. Add these lines to your application's Gemfile:

gem 'pry'
gem 'byebug'

Then execute bundle install to install the gems.

Using Pry

Pry is an interactive shell for Ruby, which allows you to inspect and manipulate code directly.

You can use it by simply adding binding.pry anywhere in your code where you want to pause execution.

def find_square_root(number)
  binding.pry
  Math.sqrt(number)
end

When your code hits binding.pry, it will pause, allowing you to inspect variables, call methods, and so on.

Using Byebug

Byebug allows you to set breakpoints in your code, step through the execution, and inspect variables.

You can start using Byebug by adding byebug anywhere in your code where you want to pause execution.

def calculate_sum(numbers)
  sum = 0
  numbers.each do |number|
    byebug
    sum += number
  end
  sum
end

3. Code Examples

Example 1: Debugging with Pry

def divide(x, y)
  binding.pry
  x / y
end

divide(10, 2)

Here, when the divide method is called, the code will pause at binding.pry. You can then inspect the variables x and y, and even modify them.

Example 2: Debugging with Byebug

def find_max(numbers)
  max = numbers[0]
  numbers.each do |number|
    byebug
    max = number if number > max
  end
  max
end

find_max([5, 3, 9, 1, 7])

Here, the code will pause at byebug during each iteration. You can inspect the number and max variables, step to the next line, or continue execution.

4. Summary

In this tutorial, we learned about two powerful Ruby debugging tools: Pry and Byebug. We learned how to pause our code execution, inspect variables, and step through the code.

For further learning, try to debug more complex code with these tools. You can also explore their advanced features, such as conditional breakpoints.

Here are some additional resources:
- Pry Documentation
- Byebug Documentation

5. Practice Exercises

Exercise 1: Write a method that calculates the factorial of a number. Use Pry to debug it.

Exercise 2: Write a method that sorts an array of numbers in descending order. Use Byebug to debug it.

Exercise 3: Write a method that finds the duplicate numbers in an array. Use both Pry and Byebug to debug it.

Happy Debugging!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Unit Converter

Convert between different measurement units.

Use tool

Image Compressor

Reduce image file sizes while maintaining quality.

Use tool

Word Counter

Count words, characters, sentences, and paragraphs in real-time.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Date Difference Calculator

Calculate days between two dates.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help