public
Last active

Use Pry as IRB replacement in rails 3 console

  • Download Gist
development.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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

Hi, what potentially causes the LoadError?

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.

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

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

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)

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..

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

reload! definitely works for me, yes

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

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

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

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

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
}

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

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

@13k thanks works great

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.