Brian Ewing

Prying into Rails

Pry is an amazing, light-weight alternative to IRB packed with nifty features.

I wanted to be able to run a rails console, but with Pry instead of IRB. I tried initially hacking at the “rails console” command, but it’s not nicely feasible as it calls IRB.start with no room for customisation without overriding the whole Rails::Console#start method (see the source code, cerca line 47)

So, a rake task will have to do. Here’s what I came up with:

1
2
3
4
5
6
7
8
9
10
task :pry => :environment do
  begin
    Pry.start
  rescue NameError
    STDERR.puts "Pry isn't installed. Place this in the development group in your Gemfile"
  end
end

task :console => :pry
task :c => :pry

This works great! Running “rake pry” drops into a Pry shell with my app at the ready. Nice :)