Bhushan Lodha
Bhushan Lodha

Reputation: 6862

How to install pry/pry-rails globally for all projects?

How can replace irb and rails console with pry/pry-console globally for every project without having to include it in a project?

Upvotes: 1

Views: 1063

Answers (1)

matthewd
matthewd

Reputation: 4420

Easy mode is to hack your .irbrc, so that when anything tries to load IRB, you take over and force it to load Pry instead:

begin
  gem "pry"
rescue => ex
  $stderr.puts ex.message
else
  require "pry"
  Pry.start
  exit!
end

If you're using Bundler, however, that'll still only work if Pry is available in the current bundle.


To make it work even when Bundler doesn't think Pry should be allowed to activate, you'll need to monkey with gem loading -- Bundler tries very hard to make it impossible.

To do that, you'll need a ~/.rubyrc.rb file, which you ensure always gets loaded for all ruby commands by exporting RUBYOPT=$HOME/.rubyrc in your .bashrc / .zshrc.

That file can then hack into Bundler's internals to force loading a non-bundled gem. You can also monkey-patch Bundler.require, which is how Rails loads all the gems in the Gemfile, to similarly force pry-rails in any application that contains rails.

(This strategy also allows you to globally support binding.pry, without needing to explicitly require anything, or add the gem to the project.)

Upvotes: 1

Related Questions