Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

This comment has been minimized.

Show comment Hide comment
@JoshCheek

JoshCheek Jul 10, 2011

Hi, what potentially causes the LoadError?

Hi, what potentially causes the LoadError?

@robbevan

This comment has been minimized.

Show comment Hide comment
@robbevan

robbevan Jul 16, 2011

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.

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

This comment has been minimized.

Show comment Hide comment
@roadhouse

roadhouse Aug 22, 2011

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

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

@13k

This comment has been minimized.

Show comment Hide comment
@13k

13k 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 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

This comment has been minimized.

Show comment Hide comment
@13k

13k 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)

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

This comment has been minimized.

Show comment Hide comment
@raven-chen

raven-chen Sep 13, 2011

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

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

This comment has been minimized.

Show comment Hide comment
@agibralter

agibralter Sep 14, 2011

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

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

@robbevan

This comment has been minimized.

Show comment Hide comment
@robbevan

robbevan Sep 14, 2011

reload! definitely works for me, yes

reload! definitely works for me, yes

@agibralter

This comment has been minimized.

Show comment Hide comment
@agibralter

agibralter Sep 14, 2011

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

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

@grosser

This comment has been minimized.

Show comment Hide comment
@grosser

grosser Oct 18, 2011

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

grosser commented Oct 18, 2011

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

@dnszero

This comment has been minimized.

Show comment Hide comment
@dnszero

dnszero Nov 30, 2011

@grosser FTW!

dnszero commented Nov 30, 2011

@grosser FTW!

@npiv

This comment has been minimized.

Show comment Hide comment
@npiv

npiv Jan 30, 2012

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

npiv commented Jan 30, 2012

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

@sheerun

This comment has been minimized.

Show comment Hide comment
@sheerun

sheerun 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

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

This comment has been minimized.

Show comment Hide comment
@quark-zju

quark-zju Mar 11, 2012

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
}

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

This comment has been minimized.

Show comment Hide comment
@l0b0

l0b0 Mar 28, 2012

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

l0b0 commented Mar 28, 2012

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

@grosser

This comment has been minimized.

Show comment Hide comment
@grosser

grosser Mar 28, 2012

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

grosser commented Mar 28, 2012

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

@lfender6445

This comment has been minimized.

Show comment Hide comment
@lfender6445

lfender6445 Mar 24, 2014

@13k thanks works great

@13k thanks works great

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