简体   繁体   English

指定要用于特定宝石的宝石版本?

[英]specify gem version to be used with a specific gem?

This is more of a general question, I'm new to rails. 这更是一个普遍的问题,我是Rails的新手。

I trying to use a gem that requires an older version of json, json -v 1.6.5 我试图使用需要较旧版本json的gem json -v 1.6.5

other gems in my rails application depends on newer version of json, json -v 1.8 我的Rails应用程序中的其他gem取决于较新版本的json,json -v 1.8

I'm wondering if it is possible to specify json version to be used with a specific gem? 我想知道是否可以指定要与特定gem一起使用的json版本?

Thank you 谢谢

No it's impossible to use 2 versions of a gem at the same time. 不,不可能同时使用2个版本的gem。

Use 2 versions of gem at same time 同时使用2个版本的gem

如果bundle更新不能解决依赖关系,我将分叉gem,您需要使用与其他gem相同的版本,然后指向我的分叉。

gem 'some_gem', git: 'https://github.com/user/My-Fork.git'

Each of your dependencies will have a version constraint and your project can only use one version of each gem. 您的每个依赖项都会有一个版本约束,并且您的项目只能使用每个gem的一个版本。

The answer to your specific question is that yes , you can control the version of an indirect dependency by specifying which version to use directly in your gemfile. 特定问题的答案是, 可以 ,您可以通过指定要在gemfile中直接使用哪个版本来控制间接依赖的版本。 However, this version must satisfy the version constraint of the direct dependency. 但是,此版本必须满足直接依赖的版本约束。

A brief example. 一个简单的例子。 Let's say your gemfile looks like this: 假设您的gemfile如下所示:

source 'https://rubygems.org/'

gem 'somegem', '~> 1.0'

And your gemfile lock looks something like this (note, some parts omitted for brevity): 您的gemfile锁看起来像这样(注意,为简洁起见,省略了一些部分):

GEM
  remote: https://rubygems.org/
  specs:
    somegem (1.0)
      json (~> 1.8)
    json (1.8)

The Gemfile.lock indicates that somegem is dependent on json and that the json version must be greater than or equal to 1.8 but less than 2.0 (read more about the ~> operator here ). Gemfile.lock指示somegem依赖于json ,并且json版本必须大于或等于1.8但小于2.0在此处了解有关〜>运算符的更多信息)。

If you want to use lets say json version 1.9 , you can modify your gemfile or use bundler commands to update the version used in the lock file. 如果要使用json版本1.9 ,则可以修改gemfile或使用bundler命令更新锁定文件中使用的版本。

Eg 例如

source 'https://rubygems.org/'

gem 'somegem', '~> 1.0'
gem 'json'   , '~> 1.9'

In your specific case, if you have two dependencies that use conflicting versions of an indirect dependency, the gems will be incompatible. 在您的特定情况下,如果您有两个使用间接依赖版本相互冲突的依赖,那么gem将是不兼容的。 However, this example is meant to show that you can specify the version of an indirect dependency IF it meets the constraint specified by the direct dependency. 但是,此示例旨在说明如果满足直接依赖项指定的约束条件,则可以指定间接依赖项的版本。

Rails uses Bundler to manage Ruby dependencies. Rails使用Bundler管理Ruby依赖项。 An overview of the gemfile would be a good place to start learning how to manage your project's dependencies. gemfile概述是开始学习如何管理项目依赖项的好地方。

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

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