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.
| Aspect | require | require_relative |
|---|---|---|
| File path type | Absolute or load path based | Relative to current file's directory |
| Search behavior | Searches Ruby's $LOAD_PATH | Does not search $LOAD_PATH |
| Common use case | Loading gems or standard libraries | Loading project files in same folder or nearby |
| Path specification | Needs full or load path | Needs relative path from current file |
| Performance | Slightly slower due to searching paths | Faster as path is explicit |
| Error if file missing | LoadError | LoadError |
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.
require './helper'
puts helper_methodrequire_relative Equivalent
The same file can be loaded more simply with require_relative by specifying the path relative to the current file.
require_relative 'helper'
puts helper_methodWhen 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.require_relative to simplify loading files within your project folder.require is necessary for external or installed libraries outside your project.LoadError if the file cannot be found.