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.
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.
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.
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.
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.
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.
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.
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.
This tutorial covered Ruby's require method with practical examples showing library loading, local files, gems, and path management techniques.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Ruby tutorials.