Ruby Require Method
last modified October 18, 2026
This tutorial explains how to use Ruby's require method to load
external code. The method is essential for modular programming in Ruby.
The require method loads a Ruby file or extension once per process.
It searches the directories in $LOAD_PATH and returns
true when it loads a file, or false when the feature
was already loaded. A missing file raises LoadError.
require caches loaded features in $LOADED_FEATURES, so a
second call returns false without executing the file again. In
contrast, load executes the file every time and needs an explicit
extension. require_relative resolves the path relative to the
current file instead of searching $LOAD_PATH. Both
require and require_relative can load native
extensions as well as Ruby files.
Basic Require Example
This simple example demonstrates loading a standard library module with require. The date module provides date manipulation capabilities.
require 'date'
today = Date.today
puts "Today is #{today}"
The require 'date' loads Ruby's date module. After requiring, we
can use the Date class directly. The method searches standard
library paths automatically.
Requiring Local Files
This example shows how to require a local Ruby file in the same directory.
require_relative resolves the path from the file containing the
call, so no load path changes are needed.
module MyLib
def self.hello(name)
"Hello, #{name}!"
end
end
require_relative 'mylib'
puts MyLib.hello("Ruby")
A plain require 'mylib' would fail unless the directory is in
$LOAD_PATH; require './mylib' works only when the
process runs from that directory. An absolute path can also be passed directly,
as in require File.expand_path('mylib', __dir__).
Return Value and Load Errors
require returns true when it loads a feature and
false when that feature is already loaded. A missing file raises
LoadError instead of returning false.
puts require 'json' # true
puts require 'json' # false
begin
require 'missing_library'
rescue LoadError => e
puts "Not available: #{e.message}"
end
The first call loads JSON and returns true; the second finds it in
$LOADED_FEATURES and returns false. The rescue clause
handles the missing library. Errors raised inside an existing file, such as
NameError, propagate normally.
Requiring Gems
RubyGems integrates with require to load installed gems. This
example shows requiring a popular HTTP library.
require 'httparty'
response = HTTParty.get('https://api.github.com')
puts response.code
After installing the gem (gem install httparty), we can require it
like any standard library. RubyGems finds the installed gem and adds its
lib directory to the load path when the gem is activated.
Conditional Requiring
We can conditionally require different implementations based on environment. This example loads either a production or development database adapter.
if ENV['RACK_ENV'] == 'production' require 'pg' else require 'sqlite3' end
The code checks an environment variable to determine which database library to
load. The selected adapter gem must be installed; a missing gem raises
LoadError.
Requiring Multiple Files
Large projects often split code across multiple files. This example shows requiring several related files from a directory.
Dir.glob(File.join(__dir__, 'lib', '*.rb')).sort.each do |file| require file end
The code builds absolute paths with __dir__, so it works regardless
of the current directory. Sorting makes the load order deterministic. A bare
relative path such as lib/a.rb would fail by default because the
current directory is not in $LOAD_PATH.
Requiring with Load Path Modification
Sometimes we need to add custom paths to $LOAD_PATH before
requiring. This example shows how to properly modify the load path.
$LOAD_PATH.unshift(File.expand_path('vendor', __dir__))
require 'custom_lib'
puts CustomLib.version
The code adds the vendor directory next to the current file to
$LOAD_PATH before requiring. unshift puts it first, so
its files take precedence. Here vendor/custom_lib.rb defines
CustomLib.version.
Source
This tutorial covered Ruby's require method with practical examples showing library loading, local files, gems, and path management techniques.
Author
List all Ruby tutorials.