Image for post
Image for post

The autoload Method in Ruby

Mehdi Farsi
Aug 27, 2018 · 3 min read

In this article we’re going to explore the following topics:

  • autoload registration & lazy loading
  • the Module#autoload? method
  • the Module#autoload method behind the scene

Feel free to have a look to the Requiring a file in Ruby article if you are not familiar with the Kernel#require method.

I’m thrilled to share with you our latest project: Fun Facts about Ruby — Volume 1

Please feel free to spread the word and share this post! 🙏

Thank you for your time!

autoload registration & lazy loading

The Module#autoload method registers a file path to be loaded the first time that a specified module or class is accessed in the namespace of the calling module or class.

Let’s detail a simple example to break down the previous assertion.

now let’s run our Ruby script

$> ruby car.rb
The Engine module isn’t yet loaded!
The Engine module is loading!
The Engine module has been successfully loaded!

Here we see that after a call to autoload(:Engine, ‘./engine.rb’) The engine.rb is not yet loaded.

In effect, this file is only loaded when we explicitly call the Engine module.

This mechanism allows Ruby to only load the files that contain the modules/classes used by the execution flow of the running program.

The Module#autoload? method

The Module#autoload? is in charge of checking if a registered module/class in a specific namespace has already been loaded or not.

It’s also used for checking if a module is registered (or not) as autoload in a specific namespace


The module B is loading!

The first call to autoload? :B returns the "./b.rb" file path.

Then the B module is loaded via a loading of the b.rb file.

Finally, the second call to autoload? :B returns nil because the B module is already loaded.

So, let’s autoload a C module outside of the the A namespace



The call to autoload? :C within the A namespace returns nil because the :C module is registered as autoload in the top-level namespace and not in the A namespace.

In contrary, the second call to autoload? :C is invoked within the top-level namespace. As it’s within the same namespace as the autoload(:C, './c.rb') registration then it returns the "./c.rb" file path.

Now that we are more familiar with the autoload registration mechanism, let’s dive into what happens behind the scene when we register and call a module.

The Module#autoload method behind the scene

When the Module#autoload method is called within a specific namespace then the module/class and the file path given as arguments of the method are stored in an internal hash table.

Let’s have a look to this example



Here, we can see that the B constant exists even if it hasn’t been loaded yet.

In effect, a call to autoload will automatically create a constant named as the first argument of the method and flagged as autoload registered. The value of this constant will be undefined and NOT nil.

When the B module will be invoked then Ruby will search for the B entry within the constants hash table of the A namespace.

It’ll then load the file using the Kernel#require method

Finally the constant hash table will unflag as autoload the B module.

Voilà !

Image for post
Image for post

Thank you for taking the time to read this post :-)

Feel free to 👏 and share this article if it has been useful for you. 🚀

Also, as I post an article every 3 days, feel free to follow me to be notified of my new releases.

Here is a link to my last article: OpenStruct in Ruby.


E-Learning platform for Ruby and Ruby on Rails

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store