rubyguides

Kernel#load

load(filename[, wrap]) -> true

The load method reads and executes Ruby code from a file. Unlike require, which tracks loaded files to prevent double-loading and is designed for libraries, load executes the file every time it’s called and is typically used for loading scripts or configuration files.

Use load when repeated execution is the point. That makes it useful for development reloads, plugin experiments, and small scripts that should run again even if the same file was loaded earlier.

Key Differences from require

Understanding when to use load versus require is important in Ruby development. require is designed for loading libraries and tracks dependencies, while load is for executing scripts where you want the code to run fresh each time. The choice comes down to intent: libraries get required once, scripts get loaded every time they are needed.

Practical Examples

basic file loading

# Load and execute a Ruby script
load "setup.rb"

# With full path
load "/path/to/scripts/initialize.rb"

The path can be relative or absolute, but it should point to a file you control. Because the file is executed as Ruby code, the contents have the same power as code written directly in the caller. Any constants, methods, or classes defined inside the loaded file become available to the program immediately after the load call returns.

loading with variable isolation

# The wrap parameter creates an isolated module
load "plugin.rb", true  # Code runs in a new module, won't affect main namespace

# Without wrap (default false), code runs in the global namespace
load "config.rb"  # Constants and methods become available globally

The wrap argument is a useful guard when you want to evaluate a file without polluting the main namespace. It does not make untrusted code safe, but it can keep constants and methods from leaking into the caller. This is helpful when loading plugin-like scripts where you want to inspect the result without letting the plugin redefine your constants.

dynamic script execution

# Load scripts from a directory
Dir.glob("scripts/*.rb").each { |script| load script }

# Conditional loading based on environment
load "development.rb" if ENV["RACK_ENV"] == "development"

This pattern is common in tools that load a set of local scripts. Keep the directory narrow and predictable so the program does not accidentally execute files that were not meant to be run. Running a glob over a scripts directory with load is a simple plugin architecture — just make sure the directory is under version control.

reloading code during development

# load executes every time, useful for reloading
loop do
  load "hot_reload.rb"  # Re-executes the file each iteration
  sleep 5
end

Reloading works because load does not cache the file. Each call reads and evaluates the file again, so changes on disk can take effect during the same process. This is the fundamental difference from requireload is built for re-execution, not for dependency tracking.

loading configuration files

# config.rb could contain:
# DATABASE_HOST = "localhost"
# DATABASE_PORT = 5432

load "config.rb"
puts DATABASE_HOST  # => "localhost"

Important Considerations

Path Resolution

load resolves paths relative to the current working directory, which means the same load call can behave differently depending on where the process was started. Always prefer full paths or paths anchored with File.expand_path when the invocation context might change.

# load uses the current directory or absolute paths
load "./lib/helpers.rb"
load "/absolute/path/to/script.rb"

# Or add to load path
$LOAD_PATH.unshift("/my/library/path")
load "library.rb"

Error Handling

Wrapping load in a begin/rescue block catches both missing files and syntax errors in the loaded code. This is especially useful when the file path comes from user input or configuration where you cannot guarantee the file exists or is syntactically valid.

begin
  load "script.rb"
rescue LoadError => e
  puts "Could not load script: #{e.message}"
rescue SyntaxError => e
  puts "Syntax error in script: #{e.message}"
end

Security Concerns

Be cautious when loading files from untrusted sources. Unlike eval, load executes the entire file, which could contain any Ruby code including system commands. Treat any file passed to load with the same skepticism you would apply to code you write yourself — the method does not sandbox or limit what the loaded code can do.

# Dangerous - don't load untrusted files!
load user_uploaded_file  # Could contain `system("rm -rf /")`

When to Use load

Use load when you need to:

  • Execute a script every time (not just once)
  • Run scripts without the library loading mechanism
  • Load configuration files that should be re-evaluated
  • Create plugin systems where code should be reloaded

Use require when loading libraries that should only be loaded once and where dependency management matters.

See Also