1. Computing

Load Vs. Require

By

Load Vs. Require

How do you load code into a Ruby program? There are two ways to do this. You're probably familiar with the ever-present require method, but what about its cousin, load? How does it differ? When should you use it?

What 'require' Really Does

You probably use the require method every day, without much thought about what it's really doing, and how it works. And first off, yes, it is a method. It is a method in the Kernel module, but in MRI it is implemented in C (it would have to be, as Ruby has no API for loading and parsing Ruby files other than methods like require and load). But beyond "load code into your program," what does it do for you?

First off, there is the $LOAD_PATH (short version, $:). This is a global variable that holds an Array object of strings, each one describing a path where files can be loaded. This will include various paths in your system where Ruby's standard library lives, as well as some other things If you're using Rubygems, when the Rubygems library itself is loaded it will hijack the require method itself. Rubygems will search through its paths, but Rubygems paths generally don't appear in $LOAD_PATH.

Second, it loads more than Ruby files. Native extensions are loaded as .so, .dll or similar. These are files that hold compiled machine code, with symbol tables that Ruby then uses to hook up to Ruby methods. These are important, it's how Ruby touches the outside world. Any Ruby bindings to any library is bound to use one of these files. And you don't even know it, you just say require 'nokogiri', it doesn't matter to you that Nokogiri was compiled natively. The 'require' method figures out the extension you really want for you.

Lastly, the require method remembers what it has already loaded. If you require the same file twice, nothing at all will happen. The first time you require it, it will load the file as normal. The second time you require it, nothing will happen. The require method itself will even return false to signify this (if something goes really wrong and the file doesn't exist, a LoadError exception will be raised). The list of required modules can be found in the $LOADED_FEATURES global variable (short version is $").

The 'load' Method

On to the load method. The load method can be thought of as requires less sophisticated cousin. It does the same job, but it doesn't do so in such a professional manner. Out of all the features outlined above, the load method only has one of these, and that's the load path. It doesn't guess which file extension you want, and it doesn't remember or even care what's been loaded or required already.

The load method also looks in the current directory for files. In Ruby 1.9.x (things were different in 1.8.x), the 'require' method will not look in the current directory for files to load. In fact, one (very small but still annoying hurdle) when upgrading to 1.9.x was this exact problem, which is solved by adding the current directory to the $LOAD_PATH global variable. If there is a file called module.rb in the current directory, require 'module' or even require 'module.rb' will not work, however require './test.rb' will. Or, slightly simpler, load 'test.rb'.

So why use load at all? Well, remember that load doesn't care if a file has already been loaded, it will gladly load the file a second time. Most of the time the declarations in that file will simple re-declare methods, classes and modules that have already been declared, which is perfectly fine in Ruby. However, there may be times when the side-effects of loading a file are desired, this is especially true when method calls are done in the file scope, loading the file again will execute those method calls again. And, of course, if for some reason the contents of the file changes between loads.

But the honest answer to the question of "When would I want to use 'load'?" is probably "not very often, if ever." It's just not that useful. Even if the file you want to load exists in the current directory, you probably still want to use 'require.' But load is there, and it might have some uses in some situations. It's yet another thing that's nice to know about, should you ever need to use it.

  1. About.com
  2. Computing
  3. Ruby
  4. Beginning Ruby
  5. Ruby's Basic Features
  6. Load Vs. Require

©2014 About.com. All rights reserved.