Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Setting up Rails or Heroku to use Pry

envygeeks edited this page · 22 revisions

Back to Main Menu

Quick Menu:

Overview

This page will show you how to configure your Rails application to use Pry instead of IRB.

With Pry in your Gemfile.

  • Rails2
    ./script/console --irb=pry

  • Rails3
    Add the following as a initializer - it provides you with access to the entire Rails stack, and all the useful Rails3 console helpers. After the initializer is in place, you can go ahead and load the console normally using rails console.

MyApplication::Application.configure do
  # Use Pry instead of IRB
  silence_warnings do
    begin
      require 'pry'
      IRB = Pry
    rescue LoadError
    end
  end
end

Without Pry in your Gemfile.

To have access to the entire Rails stack, with access to helpful Rails console commands such as reload!, you can use this snippet (placing in your ~/.pryrc file):

# Load plugins (only those I whitelist)
Pry.config.should_load_plugins = false
Pry.plugins["doc"].activate!

# Launch Pry with access to the entire Rails stack.
# If you have Pry in your Gemfile, you can pass: ./script/console --irb=pry instead.
# If you don't, you can load it through the lines below :)
rails = File.join Dir.getwd, 'config', 'environment.rb'

if File.exist?(rails) && ENV['SKIP_RAILS'].nil?
  require rails

  if Rails.version[0..0] == "2"
    require 'console_app'
    require 'console_with_helpers'
  elsif Rails.version[0..0] == "3"
    require 'rails/console/app'
    require 'rails/console/helpers'
  else
    warn "[WARN] cannot load Rails console commands (Not on Rails2 or Rails3?)"
  end
end

It will boot the Rails environment by loading RAILS_ROOT/config/environment.rb, and then load any helper commands Rails provides you with. This is a solution you can use for Rails2, or Rails3.

After you've setup the .pryrc file, you can invoke Pry normally in the root of your Rails project, with the added bonus that the Rails stack is loaded for you, plus all the useful helper console commands:

$ pry

With the pry-rails plugin (Rails >= 3 only)

This is a gem which causes rails console to open pry

Add this line to your gemfile:

gem 'pry-rails', :group => :development

bundle install and enjoy!

See the pry-rails project page for more information.

With ~/.irbrc file

This approach simply piggy-backs on IRB. If you're using a Gemfile, you should add gem 'pry', :group => :development to it.

This technique is discussed in more depth here.

  • Just add the lines below into your ~/.irbrc file.
begin
  require "pry"
  Pry.start
  exit
rescue LoadError => e
  warn "=> Unable to load pry"
end

On Heroku

To convince Heroku to use Pry as the console instead of IRB create a file called pry in your app root and then add the following to the file:

#!/usr/bin/env ruby
# To launch push this into your Git then do:
# heroku run console pry
# heroku console pry
require 'pry'
pry

After you have the above, commit it and push it, and then you can do heroku run console pry

Back to the top of the page

Something went wrong with that request. Please try again.