Using Pry on rails

Using Pry on rails

Today I am writing about ways to use Pry. Pry is IRB with incredible advantages. It let us research or investigate on the objects and classes we use.

To start with pry we need to install it on our system.

gem install pry
gem install pry-doc
gem install pry-byebug

Now, simply open your terminal and write pry, you can directly start writing Ruby code inside it. Eg:

[1] pry(main)> 2 * 2 
=> 25
[2] pry(main)>

The most powerful pry command can be ls.

image.png

We can see a list of methods titled String#methods. We can see the hash(#) symbol which means that these are instance methods. These methods are only available to instances of the string class (object).

Pry Tricks and Tips

We can do something like this on pry

[1] pry(main)> def hello
[1] pry(main)*
puts "Hello World"
[1] pry(main)* end
[2] pry(main)>
[2] pry(main)> edit -i 1

This will open the hello method inside the default editor (defined by $EDITOR environment variables) and allow us to edit it. Then pry run the code inside the pry session. Some more useful pry commands:

  • find-method
  • watch
  • backtrace
  • cat
  • ri
  • @
  • history

we can also use help for looking other useful commands for Pry.