16

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?

Jeremy Smith
  • 14,727
  • 19
  • 67
  • 114

3 Answers3

15

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.

knut
  • 27,320
  • 6
  • 84
  • 112
  • There's a lot of cases where new versions of Gems do a complete refactor and change their namespace/module names as well. The Mongo 1.x series went from `MongoClient` namespace to `Mongo::Client` and would have been a perfect use case for supporting multiple versions of the same gem. – Martin Konecny Apr 19 '16 at 19:17
  • @knut What do you mean by most actual one? – Kamesh May 03 '17 at 12:47
  • @Kamesh The gem with the highest version number. – knut May 03 '17 at 12:50
14

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.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • Great answer! Did the last sentence get truncated? ("then these gem versions...") Any chance of adding the rest? I'm keen to read to the end :-) – tjmw Oct 01 '12 at 14:22
  • 1
    @tjmw: It's not that exciting... it's just "will conflict and Bundler will tell you so." – Ryan Bigg Oct 03 '12 at 02:00
  • This seems pretty lame--can't one alias each different version into a unique namespace? Does that require one to publish the gems oneself currently? – duhaime Jan 22 '22 at 03:05
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'

wtanaka.com
  • 419
  • 4
  • 11