Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Kernel#load

(::String filename, ?::Module | bool) -> bool
load(filename, wrap=false) → true

Loads and executes the Ruby program in the file filename.

If the filename is an absolute path (e.g. starts with ‘/’), the file will be loaded directly using the absolute path.

If the filename is an explicit relative path (e.g. starts with ‘./’ or ‘../’), the file will be loaded using the relative path from the current directory.

Otherwise, the file will be searched for in the library directories listed in $LOAD_PATH ($:). If the file is found in a directory, it will attempt to load the file relative to that directory. If the file is not found in any of the directories in $LOAD_PATH, the file will be loaded using the relative path from the current directory.

If the file doesn’t exist when there is an attempt to load it, a LoadError will be raised.

If the optional wrap parameter is true, the loaded script will be executed under an anonymous module. If the optional wrap parameter is a module, the loaded script will be executed under the given module. In no circumstance will any local variables in the loaded file be propagated to the loading environment.

Ruby version changes

  1. 3.1
    • Kernel#load now accepts a module as the second argument, and will load the file using the given module as the top-level module. [Feature #6210]

See also #lambda #local_variables #JSON #loop also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0