Give it a Pry

If you are familiar with the Ruby programming language you've almost assuredly had some experience with IRB. Pry expands upon the functionality of IRB. It offers some interesting features that allow you to really dive in and explore your code. I've been using it for a little while now and already it has proven to be one of my favorite tools. Here are a few of my favorite things about Pry. Sprinkled throughout will be links to the wiki, where the Pry folks have put together some really solid documentation behind all this.

Here are the top five reasons why I switched to pry.

1.) Easy shell access

Using the shell from inside an pry session is simple, convenient, and practical. You simply prepend the command you would like to run in your shell with a period [1].

# In a rails app

 pry(main)>.ls
  Capfile Gemfile.lock  Rakefile  config     db     lib    public    test    vendor
  Gemfile README.md     app       config.ru  doc    log    script    tmp

This becomes a really convenient way to launch Vi or Textmate, run Git commands, interact with ImageMagick, et al. What's more is you can also shunt ruby output directly into your command with '#{}' like this.

pry(main)> file_path = '~/.pryrc'
    => "~/.pryrc"

  pry(main)> .ls #{file_path}
   # /Users/jjackson/.pryrc

2.) Object transparency

With Pry you get some real syntactic sugar for interacting with your objects. You can use the 'cd' command to change self to whatever you pass to it as args [2]. Once you get into the object you can use 'ls' to snoop around. If you've spent some time in the linux shell you'll be surprised at how quickly you begin to feel at home with these commands.

Don't forget you can further filter what you are searching for with 'ls' by passing a regex to the --grep flag.

pry(main)> cd Article
  pry(Article)>ls -mj --grep validate #many options here (--help to learn more), this one will list all singleton methods and search for validate methods
  #=>   _validate_callbacks

  ### at any point call 'nesting' to get an overview of where you are at.

  pry(main)> nesting

  # =>
  # Nesting status:
  # --
  # 0. main (Pry top level)
  # 1. Article

3.) View Source

Once you've got your mind wrapped around contexts and found out how easy it is to navigate your code you're ready to really pop the hood on your code. Pry offers a way to look at your source code directly via #show-method [3]. If you install pry-doc with Pry you'll find that the show-method allows you to view Ruby's C source code also. (Great tool for comparing Rubinius/MRI for example)

pry(main)>cd Pry
  pry(Pry)>show-method run_command

  # =>
  # Number of lines: 4
  #
  # def run_command(val, target = binding_stack.last)
  #   process_line(val, "", target)
  #   Pry::CommandContext::VOID_VALUE
  # end

4.) Editor Integration

Now here is my favorite feature. I've been using interactive_editor for a long time, which is a gem by jberkel inspired by Giles Bowkett. It offered a nice little convenience for opening a tempfile in your favorite editor, allowed you to edit it, and executed it when you closed the editor pane. Pry offers similar functionality [4].

# in ~/.pryrc
  Pry.config.editor = "mate"

  # then you can simply type
  pry(main)> edit -t # opens temp-file in TextMate

The difference between this and interactive_editor(ie) is that ie will save the buffer so that the next time you invoke the command it will return you to your previous state. Interactive Editor is compatible with Pry and can simply be required in your ~/.pryrc file to load it into your pry session if you want that particular functionality (Highly recommend).

That's not all!

edit-method will open the file that contains the method passed to it as its args, and place your cursor exactly where it is defined.

pry(main)>cd Pry
  pry(Pry)>edit-method -m run_command

  # File will open in your editor to line 156 of pry_class.rb ^_^

5.) Gists!!!

Push your current buffer directly to a gist.

# gist-method options
# -m, --method       Gist a method's source.
# -d, --doc          Gist a method's documentation.
# -p, --private      Create a private gist (default: true)
# -h, --help         This message

pry(Pry):1> gist-method run_command
 #=> Gist created at https://gist.github.com/1277098

That just happened!

Conclusions

Believe me when I tell you that I haven't even begun to scratch the surface of Pry with this blog post. The features outlined above are just a few methods that most naturally became part of my workflow. John Mair and the contributors to Pry have put together extensive documentation for the Pry repl and I recommend you take a look. Ryan Bates has done a railscasts that shows how you can easily incorporate pry into your Rails apps, and Josh Cheek put together an awesome cast that gives you a crash course in Pry. Links to both casts and wiki can be found below.

I hope that the brief examples above have sparked your curiosity about Pry. I have used it now for a little while and it has proven to be an indispensable tool. Thanks for reading.

***edit If you got this far you may want to check out 'Make it your own'. My follow up post which expands upon this post by giving a brief overview of Pry commands.

***edit John Mair is searching for a sponsor for Pry. Contact him at his Github page here if you know someone who might be interested. Any funds would be used to improve the website and create new casts/documentation.

(An unofficial item six would be the documentation. Everywhere I looked I found great resources for exploring Pry.)

References

1.) Shell Access
2.) Object Transparency
3.) View Source
4.) Editor Integration

Additional References

Pry RailsCast, 2011
Josh Cheek Introduction to Pry, 2011
Hacker News Pry comments, 2011
Interactive Editor

blog comments powered by Disqus