Search code examples
rubyactiverecordrubygemsbundler

Why does require choose the latest version of a gem?


If I load a gem, let's say activerecord, in IRB require chooses the latest version of activerecord. Programmatically, what is influencing this decision to choose the latest gem version? Is require doing this, or is there something in the loaded IRB that forces requires to choose the latest version?

Here are my activerecord gems installed by bundler:

➜  ~ ls -al /Users/robskrob/.rvm/gems/ruby-2.4.1/gems/activere
activerecord-4.2.10/   activerecord-5.0.0.1/  activerecord-5.1.2/    activerecord-5.1.3/    activerecord-5.1.4/    activerecord-5.1.5/    activerecord-5.1.6/    activeresource-5.0.0/

And here is an example IRB session:

➜  ~ irb
2.4.1 :001 > require 'active_record'
 => true
2.4.1 :002 > Gem.loaded_specs['activerecord'].version
 => #<Gem::Version "5.1.6">
2.4.1 :003 >

Solution

  • If I load a gem, let's say activerecord, in IRB require chooses the latest version of activerecord.

    Actually, it chooses the latest version that doesn't conflict with any already activated gem.

    Programmatically, what is influencing this decision to choose the latest gem version? Is require doing this, or is there something in the loaded IRB that forces requires to choose the latest version?

    This is requires job. More specifically, it is the job of the monkey-patched require from the RubyGems library, not the original require from the Ruby core library.

    This is just simple separation of concerns: IRb is a REPL, not a package management system, it shouldn't know anything about packages.