无法在 rbenv 环境中切换 gem 版本

Cannot switch gem versions in an rbenv environment

我在我的 macOS 10.14.4 上通过 Homebrew 安装了 rbenv 1.1.2。

我在使用与最新可用版本不同的 运行 宁 gem 时遇到问题。

例如,我安装了三个版本的xcodeproj gem(1.8.2、1.8.1、1.8.0、1.5.7)。

如果我 运行 xcodeproj --version,我会看到 1.8.2,正如预期的那样。

但是,如果我 运行 xcodeproj _1.5.7_ --version,我也看到了 1.8.2,同时期待 1.5.7。

请注意,传递版本会有所作为 - 如果我传递无效版本(例如 1.5.0),我会按预期收到 "can't find gem xcodeproj (= 1.5.0)" 错误。

我该怎么做才能运行以前的版本?

您应该使用 bundler 来管理 Gemfile 中指定的 rubygems 依赖项。例如:

$ cat Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

gem "xcodeproj", "1.5.7"

使用捆绑器安装它们

$ bundle install --path gems
Fetching gem metadata from https://rubygems.org/...............
Resolving dependencies...
Fetching CFPropertyList 3.0.0
Installing CFPropertyList 3.0.0
Fetching atomos 0.1.3
Installing atomos 0.1.3
Using bundler 1.17.2
Fetching claide 1.0.2
Installing claide 1.0.2
Fetching colored2 3.1.2
Installing colored2 3.1.2
Fetching nanaimo 0.2.6
Installing nanaimo 0.2.6
Fetching xcodeproj 1.5.7
Installing xcodeproj 1.5.7
Bundle complete! 1 Gemfile dependency, 7 gems now installed.
Bundled gems are installed into `./gems`

然后使用 bundler

执行您的 rubygem
$ bundle exec xcodeproj --version
1.5.7

请注意,当您使用 rbenv 并希望在没有捆绑程序管理的情况下执行 rubygem 时,您必须确保为每个 rbenv 版本安装您最喜欢的 gem。例如,如果您使用 2 个不同的 ruby 版本并且您喜欢 pry rubygem,那么您必须为每个 ruby 版本安装 pry。