This is more curiosity question and not a problem. When many version are present on the system which ones are chosen when require command is used? Background of the story is: I was implementing bundler gem in project (not Rails project). I had no issues but other developers had issues, after quick investigation I realized that I did not use
require "bundler/setup"
which basically loads up bundled gems. Quick fix, but it got me wondering how does ruby via rubygems decide which gems to use. Since code broke because Ruby application used older version of one of the gems, and not the newer one. Meaning it does not use the “newest” gems, so what is logic behind it?
UPDATE
To further explain this question let say you have gems foo-1.0.1 and foo-1.0.2 when you say, require 'foo' how does ruby know which one to load?
In Ruby you
requirea file rather than a gem. If that file isn’t found on the current load path, Rubygems will search the installed gems for a file with that name, and if it finds one the gem is activated (meaning that it gets added to the laod path) and the file is then required. Normally a gem will have a file with the same name in itslibdir. Only one version of a gem can be activated.The gem that gets activated is the latest version available that is compatible with any other activated gems. Normally this will just mean that the latest version installed wil be activated, but this might not be the case if you have already activated some gems which declare dependencies on earlier version of the gem you’re trying to activate.
For example, if you have
foo-1.0.1andfoo-1.0.2installed, thenrequire 'foo'(assuming they have a file namedfoo.rbin theirlibdirs and no other gem does) will cause version 1.0.2 to be activated. However if you also have a gembarwhich has a dependency on1.0.1then callingrequire 'foo'afterbarhas been activated will cause 1.0.1 offooto be activated.Futhermore, if you try to require them in the other order,
require 'foo'; require 'bar';then you will get something likeHere you can’t activate
bar, which depends on version 1.0.1 offoobecause you have already activated version 1.0.2 offoo.