Can you have multiple versions of a gem in a Gemfi

2019-02-21 13:41发布

问题:

What I would like would be something like this:

gem 'rack', '1.3.3', '1.2.4'

So that when gems require different versions of rack, they are all appeased. Is this possible?

回答1:

You can set an intervall of allowed gems

gem 'rack', '<1.3.3', '>1.2.4'

It will load the most actual one inside the selected intervall.

But I don't think you can require different gem versions. If a gem would be loaded in different versions, each class and module must get it own namespace to avoid to overwrite the methods of the gem.



回答2:

No, you are not able to have multiple gem versions loaded at the same time. This is because, as knut highlighted, the code would conflict. How would a gem know to use the 1.2.4 version of Rack as opposed to the 1.3.3 version of Rack? It can't.

Also: with Bundler, all gem dependencies must be satisfied in order for the bundling process to complete. If you have a gem that explicitly requires Rack 1.2.4 (i.e = 1.2.4 is in the gemspec for that gem) and then another gem that requires a version of Rack such as >= 1.3 then these gem versions will conflict and Bundler will tell you so.



回答3:

I came upon this question because I wanted to blacklist certain broken upstream gem versions which were buggy. While you can't do

gem 'rack', '1.3.3', '1.2.4'

you can have multiple != constraints to rule out versions that you know to be problematic:

gem 'rack', '!= 1.3.0.beta2', '!= 1.3.0.beta'