Debugging in Ruby with Pry/Byebug
Stepping through code is made possible by debugging with Pry/Byebug. It functions as a powerful debugging tool that lets you see your application’s whole state at any time. Similar to irb, Pry/Byebug interrupts the application’s execution and drops you into an interactive Ruby session. Before continuing with code execution, you can examine and change variables that are specific to the current scope during this session.
You can also read Ruby Enumerable Module: Essential Methods Every Developer
Here’s how to utilize Pry/Byebug, along with an example of code:
Installation and Setup
Install the necessary gem: The pry-byebug gem must first be installed via the command line: $ gem install pry-byebug
Require the library: At the beginning of your Ruby code, add the line require 'pry-byebug'.
Set a breakpoint: Insert the line binding.pry wherever you want the program to pause (a breakpoint).
Code Example and Commands
Take a look at the hello.rb sample below:
require 'pry-byebug'
def hello_world
puts "Hello"
binding.pry # Execution will pause here
puts "World"
end
hello_world # Calls the function
Output
Hello
World
You can also read What Is Duck Typing In Ruby With Practical Code Examples
The application will halt at the line that contains binding.pry when you run this file. Once paused, you may explore variables and control execution with particular instructions.
| Command | Action |
| step | Executes the next line of code, allowing you to step through your program. |
| Variable Name | Typing a variable’s name will display its current value. |
| exit-program or !!! | Exits the debugger session and terminates the program. |
exit (or quit) | Terminates the interactive Ruby session, allowing the program to continue running from where it left off. |
Debugging is easier when you use interactive tools like Pry/Byebug instead of spending hours finding bugs by leaving logging messages all over the place. The array of local_variables, which lists all variables local to the current method scope together with instance, class, and global variables presently in scope, is visible during these debugging sessions.
Similar to utilizing the built-in Interactive Ruby Shell (irb), Pry/Byebug expands on the fundamental idea of debugging through interactive sessions. Pry/Byebug offers a terminal inside your running application, similar to using irb within a script via binding.irb.
The breakpoint library is another comparable tool that lets you analyse and modify the state of the application by stopping execution using the breakpoint function and dropping into an interactive irb session. Additionally, you may use assert { condition } to establish conditional breakpoints, which will only call the breakpoint if the supplied block evaluates to false.
Debugging tools allow you to view exactly what condition everything is in, change settings if necessary, and then resume the plot by halting the execution, which is similar to pausing a movie to look at every object and character’s reaction during a crucial part.
You can also read IRB In Ruby: Instant Coding With The Interactive Shell
