Is it possible to require files outside the Gemfile?

老子叫甜甜 提交于 2020-01-13 13:08:42

问题


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.



来源:https://stackoverflow.com/questions/13015536/is-it-possible-to-require-files-outside-the-gemfile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!