![]() |
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. Table of Content Here’s a step-by-step guide on how to use Pry in Ruby. Install PryFirst, 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 PryYou 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.
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 PryLet’s take a simple example to demonstrate some of the features of Pry.
1. Listing MethodsAfter 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 CodeYou 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 This changes the context to the ‘Calculator’ class. ![]() Navigate code 3. Editing CodeThe 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 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 DocumentationShow 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 ![]() Viewing Documentation 5. Source CodeThe 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 This will show the implementation of the subtract method. For example, ![]() Source Code 6. Calling MethodsYou 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) ![]() 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’.
Let’s do this with a example code.
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 |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |