ruby - 有条件设置需要: true in Gemfile

标签 ruby bundler gemfile

我想根据与 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"

但这感觉不太声明(而且有点恶心)。

编辑:

澄清一下,

  1. 我希望在开发和测试环境中通过 Bundler.require 进行 pry
  2. 可在生产中使用(例如在控制台中使用),无需通过 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/

相关文章:

ruby - 对象变量名中的分号

ruby - 使用bundle install --path安装时无法找到ruby gems

ruby-on-rails - 为什么每次使用 rvm 在 rails 3.2.1 中重启后都需要 "bundle install"?

ruby-on-rails - Errno::EPERM:不允许操作@dir_s_rmdir

ruby - 我如何在我的 gemspec 中使用来自 github 的依赖项?

ruby-on-rails - Bundler无法继续;错误解析 'Gemfile' : (<unknown>)

ruby - 是否可以计算代码执行步骤?

sql - 从 Rails 6 中的 Postgres 数据库解析时间字段以检索小时和分钟?

ruby-on-rails - 如何在协作环境中处理 bundle 程序更新(Gemfile.lock)?

ruby-on-rails - Redis 支持的 Rails 模型