简体   繁体   English

如何将特定的 gem 版本设为默认?

[英]How to make a specific gem version as default?

I have two versions of ruby gem.我有两个版本的 ruby​​ gem。

json (default: 2.0.2, 1.8.6)

Here, the latest version is set to default;这里,最新版本设置为默认; however I need json 1.8.6 to be set as default.但是我需要将json 1.8.6设置为默认值。 Is there anyway to make the older versions of the gem as default?无论如何,是否可以将旧版本的 gem 设为默认值? cos I am unable to uninstall the default json version.因为我无法卸载默认的 json 版本。 Need a switch between available gem versions.需要在可用的 gem 版本之间切换。

Check what you have with:检查你有什么:

gem list json

Set the one you want:设置你想要的:

gem install --default -v1.8.6 json

This is most useful for things like "bundler"!!!这对诸如“捆绑器”之类的东西最有用!!! For other things, using bundler and a Gemfile is probably a better choice.对于其他事情,使用 bundler 和 Gemfile 可能是更好的选择。

Add添加

gem 'json', '1.8.6'

to your Gemfile or execute到您的Gemfile或执行

gem install 'json' -v 1.8.6 # may require sudo if you use system ruby

from terminal.从终端。

A Gemfile is a must but is not enough. Gemfile是必须的,但还不够。 You should also change the line你也应该改变行

require 'json'

to

require 'bundler/setup'
Bundler.require :default

This will require all the gems specified in your Gemfile that without a group .这将需要您的Gemfile中指定的所有 gems 没有group

Add this your Gemfile将此添加到您的 Gemfile

gem 'json', '1.8.6'

Now run this in your command line现在在你的命令行中运行它

bundle update

This should set your version you require这应该设置您需要的版本

Since you are using RVM to manage your Ruby versions,as you told in the comments, there is an easy solution: creating a private bundle for this application and install only the version of the gem you need in this bundle.由于您使用 RVM 来管理您的 Ruby 版本,正如您在评论中所说,有一个简单的解决方案:为此应用程序创建一个私有包,并在此包中仅安装您需要的 gem 版本。

This is how you may do it:你可以这样做:

1) Enter the directory of your application; 1) 输入您的应用程序目录;

2) Type the following command 2)输入以下命令

rvm use ruby-2.4.0@your_app_name --ruby-version --create

(I am assuming you are using Ruby 2.4.0. If this is not your version, replace it accordingly in the command above, please.) (我假设您使用的是 Ruby 2.4.0。如果这不是您的版本,请在上面的命令中相应地替换它。)

3) Install bundler gem 3) 安装 bundler gem

gem install bundler

4) Make sure your Gemfile declares the version of the gem you need. 4) 确保您的 Gemfile 声明了您需要的 gem 版本。 In this case it must have the line:在这种情况下,它必须具有以下行:

gem "json", "1.8.6"

5) Now run 5)现在运行

bundle install

And you are done!你已经完成了!

WARNING: This procedure will make sure the json gem will be version 1.8.6.警告:此过程将确保json gem版本为 1.8.6。 But you may have problems with your bundle install if some other gem installed requires a newer version of json gem .但是,如果bundle install其他一些 gem 需要较新版本的json gem ,则您的bundle install可能会出现问题。 In this case you'll have to solve this conflict some other way.在这种情况下,您必须以其他方式解决此冲突。

To learn more about different bundles for different applications, read this .要了解有关不同应用程序的不同捆绑包的更多信息,请阅读

I hope this helps.我希望这有帮助。

Context:语境:

Running a simple ruby test from CLI从 CLI 运行一个简单的 ruby​​ 测试

ruby -I test test/controllers/api_controller_test.rb

error:错误:

You have already activated json 2.0.2, but your Gemfile requires json 1.8.6.您已经激活了 json 2.0.2,但是您的 Gemfile 需要 json 1.8.6。 Since json is a default gem, you can either remove your dependency on it or try updating to a newer version of bundler that supports json as a default gem.由于 json 是默认 gem,您可以删除对它的依赖,也可以尝试更新到支持 json 作为默认 gem 的较新版本的 bundler。 (Gem::LoadError) (宝石::加载错误)

The workaround was simply running the test with bundle exec as follows:解决方法只是使用 bundle exec 运行测试,如下所示:

bundle exec ruby -I test test/controllers/api_controller_test.rb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM