Kernel#require_relative
require_relative(name) -> true or false Boolean · Updated March 13, 2026 · Kernel Methods require_relative loads an external Ruby file using a path relative to the file containing the require_relative call, not the current working directory. This makes it ideal for loading local dependencies within a project, gem, or library. Unlike require, which searches the $LOAD_PATH, require_relative resolves the path relative to the directory of the file invoking it.
The method returns true if the file was successfully loaded for the first time, or false if it had already been loaded. This behavior mirrors require, making it easy to prevent duplicate loading of the same module.
Syntax
require_relative(name) -> true or false
The name parameter can be a string representing a file path (with or without the .rb extension). Ruby automatically appends .rb if the file is not found with the exact name given.
Parameters
| Parameter | Type | Description |
|---|---|---|
| name | String | The relative path to the Ruby file to load, without the .rb extension |
Examples
Basic usage
require_relative "helpers"
Loading with output
# helpers.rb
module Helpers
def self.greet(name)
"Hello, #\{name}!"
end
end
# main.rb
require_relative "helpers"
puts Helpers.greet("World")
# => Hello, World!
Nested directory loading
# lib/utils/math.rb
module Math
module Operations
def self.add(a, b)
a + b
end
end
end
# main.rb
require_relative "lib/utils/math"
puts Math::Operations.add(5, 3)
# => 8
Checking if already loaded
result1 = require_relative "helpers"
puts result1
# => true
result2 = require_relative "helpers"
puts result2
# => false
Common Patterns
Loading gem-like library structures:
require_relative "my_app/version"
require_relative "my_app/config"
require_relative "my_app/core"
Using dir for more complex paths:
require_relative __dir__ + "/../config/environment"
Conditional loading:
require_relative "debugging" if ENV["DEBUG"]
Errors
LoadError - file not found:
require_relative "nonexistent_file"
# => LoadError: cannot load such file
LoadError - called from eval:
eval("require_relative helpers")
# => LoadError: cannot infer basepath
ArgumentError:
require_relative
# => ArgumentError: wrong number of arguments