Loading & Requiring Files
Load
The Kernel#load
method loads a file provided to it as a string argument. It returns true
if successful or raises LoadError
if file was not loaded successfully.
With load
Ruby searches for the file in the absolute path and if the file is not found it searches in the paths listed under $LOAD_PATH
(i.e. $:
). It is also permissible to provide a relative path to load
.
load
loads and makes all constants and global variables to be available in the global namespace but not local variables.
Require
The Kernel#require
method loads a file provided to it as a string argument. It returns true
if successful, false
if the file is already loaded or raises LoadError
if file was not loaded successfully and is not already loaded.
With require
Ruby searches for the file in the absolute path and if the file is not found it searches in the paths listed under $LOAD_PATH
(i.e. $:
). It is possible to provide a relative path to require
but this will search for the file in the path relative to $LOAD_PATH
and not the calling file path.
require
makes all constants and global variables to be available in the global namespace but not local variables.
Require Relative
The Kernel#require_relative
method loads a file provided to it as a string argument but as opposed to require
method does not look for the file in the absolute path or in the paths listed under $LOAD_PATH
but relatively to the path of the calling file.