Is it possible to require files outside the Gemfil

2019-05-06 23:36发布

问题:

For example, I'm developing a gem, and while I'm developing, I use pry instead of IRB, and debugger for debugging. However, I don't want possible contributors to have to install them (because they may not need them). My first idea was to put them in a Bundler group:

source :rubygems

gemspec

group :extras do
  gem "pry"
  gem "debugger"
end

And then people could use:

$ bundle install --without extras

But I want it to be a default that they're not installed. What would be perfect is that they're not in my Gemfile, but that I can still require them (if they exist on the computer). This solution would be ok because I don't care at which version they're locked. Can it be done?

回答1:

bundle install is "opt-out"—unless you specify --without some_group, it installs everything.

If you absolutely don't want to have a given gem in your Gemfile, you could just gem install that rogue gem outside of your bundle. Then it'll be visible to you under irb and straight ruby (but obviously you'll get errors if you try to require it within code running under bundle exec).



回答2:

You could add a conditional based on environment variables into the Gemfile. Example:

source :rubygems

gemspec

if ENV['WITH_EXTRAS'] == '1'
  gem "pry"
  gem "debugger"
end

The gems are then only installed/loaded, if you set the environment variable to '1' e.g. WITH_EXTRAS=1 bundle install.



标签: ruby gem bundler