Create a gist now

Instantly share code, notes, and snippets.

Use Pry as IRB replacement in rails 3 console
# Add this to the end of your development.rb and add
#
# gem 'pry'
#
# to your Gemfile and run bundle to install.
silence_warnings do
begin
require 'pry'
IRB = Pry
rescue LoadError
end
end
@JoshCheek

Hi, what potentially causes the LoadError?

@robbevan

Forgetting to add to pry to your Gemfile would cause a LoadError. Surely this is better in an initializer? (with an if Rails.env == 'development' guard). Also, that way you can git ignore the file when working in teams where others stil want use irb.

@roadhouse

i agree with @robbevan this is the best way to deal with teams setups :)

@13k
13k commented Aug 22, 2011

If you choose the initializer solution, and using rvm, you'll need to put the global gemset (supposing you installed in the global gemset) paths manually inside the initializer too, otherwise it won't work.

I choose to put the code below to config/initializers/pry.rb and add it to my ~/.gitignore

if defined?(::Bundler) and File.exist?(ENV['rvm_ruby_global_gems_path'])
  $LOAD_PATH.concat Dir.glob("#{ENV['rvm_ruby_global_gems_path']}/gems/*/lib")
end

if Rails.env.development?
  silence_warnings do
    begin
      require 'pry'
      IRB = Pry
    rescue LoadError
    end
  end
end
@13k
13k commented Aug 31, 2011

bundle console will automatically load IRB as well (I use it for gem development). So a very quick and very dirty hack I used was to put at the end of ~/.irbrc:

begin
  require 'pry'
  Pry.start
rescue LoadError
end

of course this will cause a second session to start on top of IRB's one and you'd have to exit twice to really exit (can have other issues too)

@raven-chen

My project based on another core project. when I use this. the "title" of shell become very long. e.g original is "ree-1.8.7-2011.03 :001 >"
after I use pry its become to "<@resource = "example"> *****" a giant title..

@agibralter

Has anyone noticed whether this prevents reload! from working in the rails console?

@robbevan

reload! definitely works for me, yes

@agibralter

Hmmm, for some reason my models don't get reloaded... then there must be something weird in my app environment. Thanks @robbevan

@grosser
grosser commented Oct 18, 2011

alias pryr="pry -r ./config/environment -r rails/console/app -r rails/console/helpers"

@dnszero
dnszero commented Nov 30, 2011

@grosser FTW!

@npiv
npiv commented Jan 30, 2012

much obliged grosser, that worked great for me. No messing with the actual project source

@sheerun
sheerun commented Feb 5, 2012

I use following one-liner because it leaves irb command unchanged and bundle console runs whatever you have in Gemfile (eg. bundle console development). And use pry if you want repl without Gemfile.

echo "Pry.start || exit rescue LoadError" > ~/.irbrc
@quark-zju

I use this in my zshrc:

pry () {
    if [ -e Gemfile.lock ] && [ -e ./config/environment.rb ] && grep -q pry Gemfile.lock
    then
        bundle exec pry -r ./config/environment "$@"
    else
        /usr/bin/pry "$@"
    fi
}
@l0b0
l0b0 commented Mar 28, 2012

@grosser reload! doesn't work here using the alias. Any ideas?

@grosser
grosser commented Mar 28, 2012

@l0b0 see where reload! is defined and include this too or see what else gets included to irb

@lfender6445

@13k thanks works great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment