# The Difference Between load, autoload, require, and require_relative in Ruby 📖

*Published on January 28, 2023*

---


Ruby provides a few different ways to load another Ruby program or external library. You can `load` it,&nbsp;`autoload`&nbsp;it,&nbsp;`require`&nbsp;it, or require it relative to the current directory using&nbsp;`require_relative`. Although they look and sound quite similar, there're a few important differences between them that can be confusing to understand.

After a year and a half of programming in Ruby, I still didn't quite understand all the nuances and differences between these methods. So after a few hours of study last night, here's everything I learned about loading external Ruby scripts in your programs. This post explains how they differ from each other and when you should use each. I hope you find this information helpful.

**TL;DR:**&nbsp;Use&nbsp;`load`&nbsp;to pick up any changes you made to a file while the program is running. Use `autoload`&nbsp;to speed up the initialization of your library by lazily loading the modules. Use `require` when you want to use external gems. Use&nbsp;`require_relative`&nbsp;for local files relative to the current working directory.

Let's dig deeper.

## The 'load' Method

The&nbsp;[load](https://ruby-doc.org/3.2.0/Kernel.html#method-i-load)&nbsp;method takes the&nbsp;`filename`&nbsp;as input. When the Ruby interpreter comes across the `load` method, it will load and execute the Ruby program in the file&nbsp;`filename`. If it comes across it again, it will load and execute it again, picking any new changes in the file.

> The&nbsp;`filename`&nbsp;can be either an absolute path or a relative path using&nbsp;`.`&nbsp;or&nbsp;`..`&nbsp;, and Ruby will load and run that Ruby file. If it can't find the file, Ruby will search for it in the directories listed in $LOAD\_PATH variable.

Let's create two Ruby files (in the same directory) with the following code.

```ruby
# script.rb

puts "Inside script"
load "loaded_script.rb"

puts "Inside script"
load "loaded_script.rb"

# loaded_script.rb

puts "Loaded file"
```

Running the first script gives the following output.

```bash
~ ruby script.rb

Inside script
Loaded file
Inside script
Loaded file
```

Notice that&nbsp;**each call to the**&nbsp;**`load`**&nbsp;**method executes the loaded script.**&nbsp;Hence the output&nbsp;`Loaded file`&nbsp;was printed twice.

> A call to the&nbsp;`load`&nbsp;method&nbsp;_**always**_&nbsp;loads that file. It doesn't matter if you had already loaded it earlier.

If you load a file, make a change to that file, and load it again,&nbsp;`load`&nbsp;will pick your new changes, which will&nbsp;**override the original contents**&nbsp;of the file. This is useful for making changes when a program is running and examining them immediately without reloading the application.

**Remember:**&nbsp;`load`&nbsp;will run the code every time, so if you are loading a Ruby script in multiple locations, you're doing lots of unnecessary work.

## The 'autoload' method

This method takes two arguments: a module (it can be either a string or a symbol) and a filename (string), and registers the filename to be loaded the first time that module is accessed.

```ruby
autoload(:TestModule, "lib/modules/test_module.rb")
```

Using&nbsp;`autoload`&nbsp;allows you to speed up the initialization of your library by lazily loading the modules that your code depends on.&nbsp;**It won't load the library or framework code you aren't using.**

```ruby
# script.rb

puts "Inside script"
autoload :LoadedScript, "./loaded_script.rb"
puts "Inside script"

LoadedScript.new

# loaded_script.rb

puts "Loaded file"

class LoadedScript
end
```

Running the first script outputs the following:

```bash
~ ruby script.rb

Inside script
Inside script
Loaded file
```

Note that the&nbsp;`loaded_script`&nbsp;was executed&nbsp;**only when I created an instance of**&nbsp;**`LoadedScript`****, not when I autoloaded it.**

Additionally, Ruby on Rails overrides Ruby's&nbsp;`autoload`&nbsp;method to use Rails' naming conventions, which lets you skip the second argument, i.e. the filename. To learn more about it, check out the following article: [Autoloading Modules in Rails](/autoloading-modules-in-rails)

## The 'require' method

The&nbsp;`require`&nbsp;method also loads the given file, similar&nbsp;`load`. However,&nbsp;**it won't load the file if it's already loaded.**&nbsp;Any constants, classes, modules, or global variables within the required file will be available to the calling program.

> If the filename neither resolves to an absolute path nor starts with&nbsp;`./`&nbsp;or&nbsp;`../`, the file will be searched for in the library directories listed in&nbsp;`$LOAD_PATH`.

**Requiring a library a second time won't execute it again.**

```ruby
# script.rb

puts "Inside script"
require "./required_script"
puts "Inside script"
require "./required_script"

# required_script.rb
puts "Required file"
```

Running&nbsp;`script.rb`&nbsp;provides the following output.

```text
Inside script
Required file
Inside script
```

Note that requiring the script again didn't execute it, unlike the&nbsp;`load`&nbsp;method. Also, the file extension&nbsp;`.rb`&nbsp;is optional in&nbsp;`require`, whereas&nbsp;`load`&nbsp;will loudly complain about it and crash. &nbsp;

When using&nbsp;`require`, you&nbsp;_can_&nbsp;use the&nbsp;`./`&nbsp;format for a file in the current directory, e.g.&nbsp;`require "./script"`. However, it's not a common practice. Instead, you should use the&nbsp;`require_relative`&nbsp;method. Reserve&nbsp;`require`&nbsp;for loading the external gems.

**Use**&nbsp;**`require`**&nbsp;**for loading Ruby gems**

> When you&nbsp;**`require`**&nbsp;a gem, really you’re just placing that gem’s&nbsp;**`lib`**&nbsp;directory onto your&nbsp;**`$LOAD_PATH`**.

The&nbsp;`require`&nbsp;method uses the&nbsp;`$LOAD_PATH`&nbsp;to locate the mentioned file. As the Ruby gems are installed in a directory present in&nbsp;`$LOAD_PATH`, this allows you to require them without providing the path.

```ruby
require 'minitest'
```

Once you've required&nbsp;`minitest`, RubyGems automatically places its&nbsp;**`lib`**&nbsp;directory on the&nbsp;**`$LOAD_PATH`**.

That's how Ruby gems work. You write the gem's Ruby code into the&nbsp;**`lib`**&nbsp;directory containing a Ruby file with the same as your gem. For the&nbsp;`minitest`&nbsp;gem, the file will be&nbsp;**`minitest.rb`****.**&nbsp;Additionally, the&nbsp;**`lib`**&nbsp;directory contains a directory with the same name as the gem containing the rest of the files.

```bash
% tree minitest/
minitest/
└── lib/
    ├── minitest/
    │   ├── code.rb
    │   ├── app.rb
    │   └── ...
    └── minitest.rb
```

Since the&nbsp;`lib`&nbsp;directory is placed into&nbsp;`$LOAD_PATH`, you can require the gem without providing the path. Ruby will know where to load it from.

## The 'require\_relative' method

In the previous example, we provided the path to the required file as&nbsp;`./required_script`, that is, using the&nbsp;`.`&nbsp;to indicate the current directory. The&nbsp;`require_relative`&nbsp;method loads the external file by searching relative to the file from which it's called. This allows you to write:

```ruby
require_relative "required_script"
```

The code will result in the same output as&nbsp;`require`; they work in the same way.

`require_relative`&nbsp;is useful when you want to navigate the local directory hierarchy as follows:

```ruby
require_relative "lib/rails/server"
```

Use&nbsp;`require_relative`&nbsp;for local files relative to the current working directory.

**Confused?**

At this point, you might be really confused about the exact difference between&nbsp;`require`&nbsp;and&nbsp;`require_relative`. It feels like they both are doing the same thing. I was confused, too. So I did some further digging and came across this excellent StackOverflow answer that elegantly explains how they differ and how to choose one over the other: [Ruby ‘require’ error: cannot load such file](https://stackoverflow.com/questions/9750610/ruby-require-error-cannot-load-such-file/9750732#9750732)

I highly recommend reading the full answer, but here's a gist of it.

1. `require_relative 'script'`&nbsp;assumes that the relative path between the two Ruby source files will stay the same.
2. `require 'script'`&nbsp;assumes that&nbsp;`script`&nbsp;is inside one of the directories on the load path (`$LOAD_PATH`). This generally requires extra setup, since you have to add something to the load path.
3. `require './script'`&nbsp;assumes that the relative path from the Ruby process's current working directory to&nbsp;`script`&nbsp;is going to stay the same.

Hope that clears things a bit.

## Conclusion

1. Use&nbsp;`load`&nbsp;to pick up any changes you made to a file while the program is running.
2. Use&nbsp;`autoload`&nbsp;to speed up the initialization of your library by lazily loading the modules.
3. Use&nbsp;`require`&nbsp;when you want to use external gems.
4. Use&nbsp;`require_relative`&nbsp;for local files relative to the current working directory.

