Horje
How to use pry in Ruby?

Debugging and exploring code is a crucial part of the software development process. For Ruby developers, the standard Interactive Ruby (IRB) shell offers basic functionalities but lacks some advanced features that can significantly enhance the debugging experience. This is where Pry comes in.

Pry is an alternative IRB shell that provides a more powerful and flexible environment for interactive programming like REPL(Read-Eval-Print Loop). It offers a range of advanced features such as syntax highlighting, command navigation, runtime invocation, and a robust plugin architecture.

Here’s a step-by-step guide on how to use Pry in Ruby.

Install Pry

First, you’ll need to install Pry if you haven’t already. You can install it using the gem command.

gem install pry

gem install pry

Basic Usage of Pry

You can start a Pry session in your terminal by typing ‘pry’ and will be able to execute code snippets.

start a pry session

To use Pry in your Ruby application, you need to require it and then start a Pry session. Here’s a simple example.

Ruby
require 'pry'
class Sample
  def initialize(name)
    @name = name
  end

  def greet
    binding.pry #Start a Pry session
    puts "Hello, #{@name}!"
  end
end

sample = Sample.new("Alice")
sample.greet

Run this Ruby program by using ‘ruby <filename>.rb’. It will start a Pry a session after executing ‘binding.pry’. In this session you can inspect the variables and can change their values. For example,

Start a pry session

Here, you can check the value of any variable by writing it’s name and change it’s value using assignment operator. After ‘exit’ command the pry session will end the program will continue it’s execution.

Key Features of Pry

Let’s take a simple example to demonstrate some of the features of Pry.

Ruby
require 'pry'

class Calculator
  def add(a, b)
    a + b
  end

  def subtract(a, b)
    a - b
  end

  def multiply(a, b)
    a * b
  end

  def divide(a, b)
    a / b
  end
end

calc = Calculator.new
binding.pry
result = calc.add(10, 5)
puts "Result: #{result}"

1. Listing Methods

After running this code a Pry session will start. The ‘ls’ command lists methods and constants available in the current context. We have used ‘ls Calculator’ since we have started the session outside the class. For example,

listing methods

2. Navigating Code

You can change context to a specific class or object using the ‘cd’ command. For example, to navigate to the Calculator class,

[1] pry(main)> cd Calculator
[2] pry(Calculator):1>

This changes the context to the ‘Calculator’ class.

Navigate code

3. Editing Code

The edit command opens the default text editor to modify the source code of a method. For example, to edit the add method,

[1] pry(main)> cd Calculator
[2] pry(Calculator):1> edit add

A new VS code window will be opened where you can make changes to your code. For example, Here a print statement is added like ‘puts “Adding #{a} and #{b}”‘ to the add method. For example,

pry edit

4. Viewing Documentation

Show documentation for ‘multiply’ using the below command. It will display information about ‘multiply’ method if you have added a comment before this method.

[1] pry(main)> cd Calculator
[2] pry(Calculator)> show-doc multiply

Viewing Documentation

5. Source Code

The show-source command displays the source code of a specific method. For instance, to see the code for the subtract method.

[1] pry(main)> cd Calculator
[2] pry(Calculator)> show-source subtract

This will show the implementation of the subtract method. For example,

Source Code

6. Calling Methods

You can call methods directly in the Pry console. For example, to call the multiply method with arguments 3 and 4, we will have to create a instance of our ‘Calculator’ class which is ‘calc’.

[1] pry(main)> calc.multiply(3, 4)
=> 12

Calling Methods

7. Plugins

‘pry-byebug’, enhances Pry with debugging capabilities, allowing you to step through code, set breakpoints, inspect variables, and more. To install, use this command.

gem install pry-byebug

Add a ‘require’ line for this plugin in your code.

require 'pry-byebug'

After reaching binding.pry, the script pauses, and you can use pry-byebug commands for debugging like ‘step’ and ‘break’.

  • Step: Execute the next line of code.
  • Break: Set breakpoints at specific lines.
  • Continue: Resume execution until the next breakpoint or the end of the script.

Let’s do this with a example code.

Ruby
require 'pry'
require 'pry-byebug'  # Include pry-byebug plugin

class Person
  def initialize(name, age)
    @name = name
    @age = age
  end

  def introduce
    "Hello, my name is #{@name} and I am #{@age} years old."
  end

  def celebrate_birthday
    @age += 1
  end
end

# Create an instance of Person
person = Person.new("Alice", 30)

# Set a breakpoint here
binding.pry

# Debugging session begins

# Example: Calling methods and debugging
puts person.introduce
person.celebrate_birthday
puts "After celebrating a birthday: #{person.introduce}"

The script will pause at ‘binding.pry’. In Pry, type ‘step’ to execute the next line of code.

step command

You can set a breakpoint using the ‘break’ command followed by the line number where you want to pause execution.

[1] pry(main)> break 17

break command

Use ‘continue’ to resume execution until the next breakpoint or the end of the script.

[1] pry(main)> continue

Continue Command




























Reffered: https://www.geeksforgeeks.org


Ruby

Related
How to Connect MySQL with Ruby on Rails? How to Connect MySQL with Ruby on Rails?
How to check database connection in Ruby? How to check database connection in Ruby?
How to access private method in Ruby? How to access private method in Ruby?
How to Convert a UNIX Timestamp (Seconds Since Epoch) to Ruby DateTime? How to Convert a UNIX Timestamp (Seconds Since Epoch) to Ruby DateTime?
How to Create a Linked List in Ruby? How to Create a Linked List in Ruby?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17