I have to confess that I’m generally skeptical about alternatives for tools that work so fine like IRB. And I really like IRB. So why Pry? And why everywhere? Because Pry features blew my mind. When I wrote about IRB customization, I was doing what I always do when I like a tool: customize it in order to feel the tools more familiar with my way of thinking. So, when I run into Pry, some of its features blew my mind because they were exactly what I really would like to have in IRB. For example, sometimes you would like to explore a class or an object quickly. The way you can do it with Pry feels just natural. It just seems the right way too do it:
Toy is a little method I have in my .pryrc (and previously in my .irbrc) that I use when I want to play with arrays. Pry comes with wonderful commands like cd that operates both with classes and instance objects or like ls that you can use to list all the class methods (-m option) or instance methods (-M option). And a very long list of other terrific features as editor integration, shell integration or gist integration. But I don’t need to persuade you to use Pry because I’m sure you will use it after taking a look at these wonderful resources.
So the title of this post is “Pry everywhere” and now I’m going to show you quickly what I’ve done to migrate to Pry. There are a lot of solutions about this topic but all of them involve something I don’t like especially about rails integration. My requirements were fairly simple:
- I don’t want to lose the customizations I’ve done with IRB
- The same for rails console
- I don’t want to add any gem (although this) to my Gemfile in rails projects.
After a bit of researching, I came up with the following solution:
My current .irbrc:
Pratically, everytime I start IRB I will start a Pry session. It feels like a dirty solutions and I have to confess I don’t know if it has any issues. For now, it’s working just fine with my requirements. The bundler code is necessary to require pry and other gems from rvm global gemset in a rails console without declaring them in the Gemfile. Then, in the .pryrc I have:
If you compare this file with my previous .irbrc you’ll notice that this one is shorter. This means that Pry is doing a piece of the job I would like to have by default, like colors and history commands. My .railsrc is very similar to the previous one but it has a different that could interest you if you are an hirb user:
This way, Hirb is working flawlessly. And the combination of Rails and Pry is just fantastic. Give it a try. I have been able to migrate to Pry with a fair effort, hoping this kind of configuration can help you too!