强制 Bundler 安装本地 Ruby 版本支持的 gem

Force Bundler to install gems supported by the local Ruby version

有没有办法在不指定依赖项的情况下安装最新支持的版本?

我在使用 activesupport gem 时遇到问题。最新版本 (5.0.0.1) 支持 Ruby >= 2.2.2。如果我指定我需要像这样的 gem '~> 4.2' Bundler 将尝试安装版本 5,即使我使用的是 Ruby 2.0。指定确切的版本 4.2.7.1 或设置最大值 '~> 4.2', '< 5' 有效,除非将 gem 与 Rails 一起使用 5.

有没有办法根据当前 Ruby 版本管理 gem 版本?

显然新版本的 Bundler 会自动为您做这件事。 我发现来自 André Arko 的 this comment 提到这已经包含在最新的 RC 版本中。

我在我的 Gemfile 中指定了 Ruby '2.0',使用 gem install bundler --pre 安装了 Bundler(它安装了 bundler-1.13.0.rc.2)并且 bundle install 成功安装了 activesupport 4.2。 7.1.

使用 Bundler 1.12.5 时出现以下错误:

An error occurred while installing activesupport (5.0.0.1), and Bundler cannot continue.

请注意,虽然需要更多的手动操作,但您也可以在 Gemfile 中包含逻辑:

if RUBY_VERSION <  "2.2.2"
  gem "activesupport", "4.2.7.1"
else
  gem "activesupport", "5.0.0.1"  
end