pry-byebug become a power user
Tags: rails, ruby
Published: 2023-07-16 06:23:01

We've used byebug at work, but someone has the idea to introduce pry-byebug. Byebug is cool but lacks visual help for debugging. So having pry-byebug was cool for that. Nevertheless, the behavior was different. 

I decided to become a power user of pry and pry-byebug and write an article about it. 

## Let's Install it!

Let's begin as usual with a Rails app
```shell
rails new test-&pp
```

Go to your Gemfile and add :
```
gem 'pry-byebug'
```

If you only add pry, you cannot debug with pry.

## Start debugging 

To start debugging with pry-byebug, you can put a binding. Pry anywhere in your code where you want to debug. 

```ruby
module Queries
  class UserQuery < Queries::BaseQuery
    
    argument :id, ID, required: false
    type Types::UserType, null: true

    def resolve(input)
      binding.pry
      User.find(input[:id])
    end
  end
end
```

This will pop a pry REPL in your rails server terminal : 

```shell
pry(#<Queries::UserQuery>)>
```

What is a REPL? 

```
A read–eval–print loop (REPL) is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user. (Wikipedia)
```


## The world is yours 

When you have your command line, you can do whatever you want. 
But be careful the command are a bit different to Byebug's ones.

### Debugging and Customizing your pry-byebug 

You can provide custom alias commands to pry with .pryrc. 
Indeed if you have used byebug, the commands available, especially their shortcut, do not exist or behave the same. 
To add a command alias at the root base of your app, you can add the .pryrc file.


```ruby
./app/.pryrc
if defined?(PryByebug)
  Pry.commands.alias_command 'c', 'continue'
  Pry.commands.alias_command 's', 'step'
  Pry.commands.alias_command 'n', 'next'
  Pry.commands.alias_command 'f', 'finish'
  Pry.commands.alias_command 'v', 'ls -l'
  Pry::Commands.command /^$/, "repeat last command" do
    pry_instance.run_command Pry.history.to_a.last
  end
end
```


These command alias definitions are documented in the pry [documentation](https://github.com/deivid-rodriguez/pry-byebug).

Let's dive into the last two commands : 

```ruby
Pry.commands.alias_command 'v', 'ls -l'
```

It will show you the local variables of your instance. 

This one :  

```ruby 
 Pry::Commands.command /^$/, "repeat the last command" do
    pry_instance.run_command Pry.history.to_a.last
  end
```

This will enable you to repeat the last command, for example, you press c to go to the next breakpoint, then if you press enter, it will act as if you press c. 

Now you can use your `pry-byebug` debugger almost like your byebug. Be careful because the alias_command will be listen over your variable. So avoid to have variables named c, v or s for example. But this is not a good pattern anyway. 

## Conclusion

This was a simple article, yet writing it helped me to use pry and pry-byebug better. 
If you have any questions, please do not hesitate to reach me on LinkedIn or Twitter or comment below 👇.

## Resources 

This is an excellent article about pry http://kyrylo.org/2013/05/30/so-what-is-binding-pry-exactly

### Keep in Touch 

On Twitter : [@yet_anotherDev] (https://twitter.com/yet_anotherDev)

On Linkedin : [Lucas Barret](https://www.linkedin.com/in/lucas-barret-67194b164/)