Bird
Raised Fist0
RubyComparisonBeginner · 4 min read

Require vs require_relative in Ruby: Key Differences and Usage

require loads external files using an absolute or load path, searching Ruby's load paths, while require_relative loads files relative to the current file's location. Use require_relative for files in the same project folder and require for installed gems or system libraries.
⚖️

Quick Comparison

This table summarizes the main differences between require and require_relative in Ruby.

Aspectrequirerequire_relative
File path typeAbsolute or load path basedRelative to current file's directory
Search behaviorSearches Ruby's $LOAD_PATHDoes not search $LOAD_PATH
Common use caseLoading gems or standard librariesLoading project files in same folder or nearby
Path specificationNeeds full or load pathNeeds relative path from current file
PerformanceSlightly slower due to searching pathsFaster as path is explicit
Error if file missingLoadErrorLoadError
⚖️

Key Differences

require loads files by searching through Ruby's $LOAD_PATH, which is a list of directories Ruby checks to find the file. This means you can require installed gems or standard libraries without specifying full paths. However, if you want to load a file in your project, you must provide a path relative to one of these directories or an absolute path.

On the other hand, require_relative loads files relative to the file where it is called. It does not use $LOAD_PATH. This makes it ideal for loading files within the same project folder or subfolders without worrying about the global load path. It simplifies requiring project files by using paths relative to the current file.

In summary, require is best for external libraries and gems, while require_relative is best for your own project files organized nearby. Both raise a LoadError if the file is not found.

⚖️

Code Comparison

Using require to load a file named helper.rb located in the same directory requires specifying the relative path or modifying $LOAD_PATH.

ruby
require './helper'

puts helper_method
Output
Hello from helper!
↔️

require_relative Equivalent

The same file can be loaded more simply with require_relative by specifying the path relative to the current file.

ruby
require_relative 'helper'

puts helper_method
Output
Hello from helper!
🎯

When to Use Which

Choose require when loading gems, standard libraries, or files installed in Ruby's load paths. It is the standard way to include external code.

Choose require_relative when loading files within your own project that are located near the current file. It avoids path confusion and is simpler for project organization.

Using require_relative improves clarity and reduces errors when working with project files, while require is necessary for external dependencies.

✅

Key Takeaways

require loads files using Ruby's global load paths, ideal for gems and libraries.
require_relative loads files relative to the current file, perfect for project files.
Use require_relative to simplify loading files within your project folder.
require is necessary for external or installed libraries outside your project.
Both raise LoadError if the file cannot be found.