我想根据与 Bundler.require
结合使用的环境,在 Gemfile 定义中有条件地设置 require:
。例如。我希望 pry 在所有环境中都可用,但只在开发和测试中设置了 require: true
。我目前正在做这样的事情:
# make pry available for anyone who wants it
# but not automatically required
# e.g. in a console
gem 'pry', require: false
# automatically require pry
# for easy usage in tests
group :development, :test do
gem 'pry', require: true
end
这有效,但会产生关于重复 gem 定义的警告:
Your Gemfile lists the gem pry (>= 0) more than once. You should probably keep only one of them. While it's not a problem now, it could cause errors if you change the version of just one of them later.
或者,我可以尝试做这样的事情:
gem 'pry', require: ENV["RACK_ENV"] != "production"
但这感觉不太声明(而且有点恶心)。
编辑:
澄清一下,
- 我希望在开发和测试环境中通过
Bundler.require
进行 pry - 可在生产中使用(例如在控制台中使用),无需通过
Bundler.require
自动要求
最佳答案
当您希望安装 gem 但不“必需”时,可以使用 :require => false
。如果您不打算在生产中使用“pry”gem,那么在生产中安装它就没有意义。
您可以通过显式指定 gem 使其可在任何环境中使用。 例如。使 pry 在开发和测试环境中可用
group :development, :test do
gem 'pry'
end
希望,这有帮助!
关于ruby - 有条件设置需要: true in Gemfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192126/