Ruby Require Method
last modified April 27, 2025
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 Ruby files or extensions only once. It
searches in the $LOAD_PATH
for the specified file. The method
returns true
if successful, false
if already loaded.
Unlike load
, require
prevents multiple loads of the
same file. It's ideal for loading libraries and extensions. The method handles
both .rb and platform-specific extensions.
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. We first need to adjust the load path or use relative paths.
require_relative 'mylib' puts MyLib.hello("Ruby")
The require_relative
looks for files relative to the current file's
location. This is safer than modifying $LOAD_PATH
for local files.
Checking Require Success
The require
method returns a boolean indicating if loading
succeeded. We can use this to handle missing dependencies gracefully.
if require 'json' puts "JSON module loaded successfully" else puts "JSON module not available" end
The code checks the return value of require
to confirm loading.
This pattern is useful for optional dependencies or fallback implementations.
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 adds gem paths to $LOAD_PATH
.
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. This pattern is common in configuration management.
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('lib', '*.rb')).each do |file| require file end
The code uses Dir.glob
to find all .rb files in the lib directory.
Each file is then required individually. This automates loading many files.
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', __FILE__)) require 'custom_lib' puts CustomLib.version
The code adds a vendor directory to the load path before requiring. Using
unshift
ensures our path is searched first. Always expand paths
properly.
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.