This page will show you how to configure your Rails application to use Pry instead of IRB.
This is a gem which causes
rails console to open
Add this line to your gemfile:
gem 'pry-rails', :group => :development
bundle install and enjoy!
See the pry-rails project page for more information.
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
MyApplication::Application.configure do # Use Pry instead of IRB silence_warnings do begin require 'pry' IRB = Pry rescue LoadError end end end
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
# 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:
You may find that the console helpers (
reload! et al.) are missing from pry in Rails 3.2+ if you are not using the
pry-rails gem. As a workaround, add something like this to your
if defined?(Rails) && Rails.env extend Rails::ConsoleMethods end
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.
begin require "pry" Pry.start exit rescue LoadError => e warn "=> Unable to load pry" end
Heroku users with Rails apps, do not need to do this, this method is reserved for people using Sinatra or EM as their framework. For Rails you simply need to add pry-rails and pry to your Gemfile, bundle install and then push and run
heroku run console and it will load pry with your Rails environment as expected since Heroku can detect Rails apps and is designed to work well with them
To convince Heroku to use Pry as the console instead of IRB add
gem 'pry' to your Gemfile and create a file called pry with.
#!/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 If you would like to shortcut it (because you are forgetful or otherwise) you can create a file called heroku in your home folder bin (make sure to change the path to your gem bin unless you are on Ubuntu 11.04+ or Debian unstable+) with the following:
#!/usr/bin/env bash # chmod +x set -e if [ "$2" == "console" ]; then exec /usr/local/bin/heroku run console pry else /usr/local/bin/heroku $@ fi