Method: Kernel#require
- Defined in:
- load.c
#require(name) ⇒ Boolean
Loads the given name
, returning true
if successful and false
if the feature is already loaded.
If the filename neither resolves to an absolute path nor starts with ‘./’ or ‘../’, the file will be searched for in the library directories listed in $LOAD_PATH
($:
). If the filename starts with ‘./’ or ‘../’, resolution is based on Dir.pwd.
If the filename has the extension “.rb”, it is loaded as a source file; if the extension is “.so”, “.o”, or the default shared library extension on the current platform, Ruby loads the shared library as a Ruby extension. Otherwise, Ruby tries adding “.rb”, “.so”, and so on to the name until found. If the file named cannot be found, a LoadError will be raised.
For Ruby extensions the filename given may use “.so” or “.o”. For example, on macOS the socket extension is “socket.bundle” and require 'socket.so'
will load the socket extension.
The absolute path of the loaded file is added to $LOADED_FEATURES
($"
). A file will not be loaded again if its path already appears in $"
. For example, require 'a'; require './a'
will not load a.rb
again.
require "my-library.rb"
require "db-driver"
Any constants or globals within the loaded source file will be available in the calling program’s global namespace. However, local variables will not be propagated to the loading environment.
1026 1027 1028 1029 1030 |
# File 'load.c', line 1026 VALUE rb_f_require(VALUE obj, VALUE fname) { return rb_require_string(fname); } |