When to use `require`, `load` or `autoload` in Ruby?
Solution 1
Generally, you should use require
. load
will re-load the code every time, so if you do it from several modules, you will be doing a lot of extra work. The lazyness of autoload
sounds nice in theory, but many Ruby modules do things like monkey-patching other classes, which means that the behavior of unrelated parts of your program may depend on whether a given class has been used yet or not.
If you want to make your own automatic reloader that loads your code every time it changes or every time someone hits a URL (for development purposes so you don't have to restart your server every time), then using load
for that is reasonable.
Solution 2
mylibrary.rb
puts "I was loaded!"
class MyLibrary
end
Try in irb
irb(main):001:0> require 'mylibrary'
I was loaded!
=> true
irb(main):001:0> autoload :MyLibrary, 'mylibrary'
=> nil
irb(main):002:0> MyLibrary.new
I was loaded!
=> #<MyLibrary:0x0b1jef>
See the difference.
Solution 3
Apart from what others have already told you, future of autoload
is uncertain. It was scheduled to be deprecated in Ruby 2.0, but the deprecation wasn't made in time for the 2.0 feature freeze. It is now expected that autoload
will be deprecated in Ruby 2.1, but that is not even certain anymore.
user3137701
Garrison Keillor of Code. Judge Dredd of the Monad Laws.
Updated on July 08, 2022Comments
-
user3137701 almost 2 years
I understand the subtle differences between
require
,load
andautoload
in Ruby, but my question is, how do you know which one to use?Other than being able to "wrap" a
load
in an anonymous module,require
seems to be preferred.But then
autoload
allows you to lazy load files -- which sounds fantastic but I'm not sure practically what you gain overrequire
Is one method preferred over the other? Is there a situation where one method stands out?
-
oligan over 12 yearsThe question asker knows the difference between require and autoload, he was asking when you should use one rather than the other.
-
crazycrv over 12 yearsThanks Andrew for pointing it out. You are right. As mentioned by user979339 above ..autoload should be used where time is critical and still you want to speed up the process. For example in the application servers like "thin". If you look at the thin source code then you will find out that the author has used autoload instead of require to load thin's core parts like command, connection and logging this speed ups thins initialization.
-
Theymiss Developer about 10 yearsSee Boris' answer below which contains a link to a more recent discussion regarding autoload. It's still present in 2.1: ruby-doc.org/core-2.1.0/Module.html#method-i-autoload
-
Douglas G. Allen over 8 yearsYou have to be careful about libs that aren't registered with Ruby stdlib and rubygems so require './filename-no ext' as an example for a local lib. Maybe you could fix this in your code and then delete my comment? But thanks for the simple example. But what about 'load'?
-
Raj about 8 yearsNo, it's still very much a part of Ruby 2.3.0: ruby-doc.org/core-2.3.0/Kernel.html#method-i-autoload, but according to the discussion in this bug (bugs.ruby-lang.org/issues/5653) the likely target will be Ruby 3.0.
-
ebrohman over 7 yearsthe autoload link is dead
-
echan00 over 6 yearsSo it is normal to have 'load' for development and 'require' for production? With an if else statement?
-
Brian Campbell over 6 years@echan00 no, you should use
require
for both development and production.load
should only be used if you are writing something special like your own reloader; but there are already plenty of good reloaders that already exist for development purposes. Rails includes one, there's one available for Sinatra, etc. If you userequire
in your code, the reloaders will behave correctly. -
Justin Ko about 5 yearsAs of Feb 2019, Matz has decided to keep
autoload
(bugs.ruby-lang.org/issues/5653#note-46). -
chemturion over 4 yearsThat feature thread has since been closed with the resolution that
autoload
will not be deprecated. bugs.ruby-lang.org/issues/5653#note-46