What is PRY? Why do we need PRY? Why read an article on PRY? Some of these questions might pop up in your mind when you read the title of this blog. Let me explain why you should be knowing about PRY.
PRY is an attempt to remake IRB, the interactive Ruby interpreter, in a way that makes more sense for mordern programmers. Some of the most important features of PRY are syntax highlighting, code indentation and code debugging. In other words, instead of coding inside a REPL session, you can start a REPL session inside your code execution with the help of PRY.
Since PRY is a Ruby gem, installing it is straightforward. The following commands will work:
$ gem install pry
PRY colorizes the syntax as it is typed into the console just like most mordern editors. In addition to this, PRY also auto-indents code thus allowing ensuring that the ‘end’ words line up with the lines that open the block.
pry(main) > class User
pry(main)* def greet
pry(main)* puts "Hello world"
Another cool feature of PRY, as per my opinion, is it’s ability to show a method’s documentation and source code inside REPL. Having this feature inside PRY lets us bypass using
ri to search for method documentations. Since PRY directly extracts it from source code, we don’t require RDoc to be installed. The documentation and/or source code can be viewed inside PRY using
PRY as Debugger
Coming to one of the most important features of PRY, its role as a debugger during code execution. Generally,
puts statements are used to output current values for debugging purposes or bona-fide debuggers are required. When using PRY, it automatically opens an IRB session at the current execution point and makes the whole program state, at that specific point, available.
If you want to specify exactly when the
binding.pry will crack open a pry-session. I do this with Ruby code conditionals:
binding.pry if session[:added_to_cart] == false
binding.pry if @first_name == "Jeena"
binding.pry if iteration > 5
To use PRY, the Gemfile of your Rails application should contain
gem pry. Take care to place PRY under the development group. Once this is done, you can add the line
binding.pry inside your code. Whenever Ruby interpreter executes that line, it stops further execution and opens up a PRY REPL session at that point. In case you want PRY sessions to open under certain conditions, you could do the following with Ruby code conditionals:
Interactive modification of runtime state
It might be convenient to open a PRY session in the middle of an executing program to modify the runtime state. Using
ls commands, the navigation feels natural and familiar. You can also pop in and out of objects, nesting sessions as deeply as you like.
PRY can be easily customized using an user’s
.pryrc file. Starting from the color of the prompt, to enabling/disabling of history or specifying the editor to be used with PRY are some of the configurations that you can set. Advanced users can even set the Input and Output objects from using the default values of
As a conclusion to this article, I would say that these are just some points of PRY that were interesting to me. For a more thorough reading, you could look up the following sites: