Question
In Ruby, what is the difference between require and include?
My question is similar to the common discussion about the difference between include and extend in Ruby.
If I want to use the methods from a module inside my class, should I use require, include, or both?
Short Answer
By the end of this page, you will understand that require and include solve different problems in Ruby. require loads code from a file, while include mixes a module's instance methods into a class. You will also see when both are needed together, which is the most common real-world case.
Concept
require and include are often confused because they are both used when working with modules, but they do very different jobs.
require: load code from a file
require tells Ruby to load and execute another file once.
require 'json'
This makes the code in that file available to your program. Ruby uses require for standard libraries, gems, and your own files.
If the module or class is defined in another file, Ruby cannot use it until that file is loaded.
include: mix a module into a class
include takes a module and adds its instance methods to a class.
module Greeting
def hello
"Hello"
end
end
class Person
include Greeting
end
Now instances of can call .
Mental Model
Think of require as bringing a toolbox into the room.
- If the toolbox is still in the garage, you cannot use the tools.
requirebrings the toolbox in so Ruby can see it.
Think of include as taking tools from the toolbox and attaching them to your class.
- The toolbox may be present.
- But until you attach the tool, your class does not gain that behavior.
So:
require= load the fileinclude= add module methods to instances of the class
In many cases, you need both: first bring in the toolbox, then attach the tool.
Syntax and Examples
Core syntax
Load a file
require 'json'
require_relative 'greeting'
requireloads libraries or gems by name.require_relativeloads a file relative to the current file.
Include a module in a class
module Greeting
def hello
"Hello"
end
end
class Person
include Greeting
end
person = Person.new
puts person.hello
Output:
Hello
Example using both together
Suppose you have a file named greeting.rb:
module Greeting
def
Step by Step Execution
Consider this code:
module Greeting
def hello
"Hello"
end
end
class Person
include Greeting
end
person = Person.new
puts person.hello
Step-by-step
1. Ruby reads the module definition
module Greeting
Ruby creates a module named Greeting.
2. Ruby defines the hello method inside the module
def hello
"Hello"
end
At this point, Greeting has an instance method named hello.
3. Ruby reads the class definition
Real World Use Cases
1. Using standard library features
require 'json'
data = JSON.parse('{"name":"Ava"}')
puts data["name"]
Here, require loads Ruby's JSON library.
2. Sharing behavior across multiple classes
module Trackable
def track
"Tracking activity"
end
end
class User
include Trackable
end
class Order
include Trackable
end
Here, include avoids duplicating the track method in multiple classes.
3. Loading app files in a multi-file project
require_relative 'trackable'
class User
Real Codebase Usage
In real projects, developers usually use require for loading dependencies and include for composing behavior.
Common pattern: load then mix in
require_relative 'authenticatable'
class User
include Authenticatable
end
This is the normal pattern when a module is defined in another file.
Organizing shared behavior
Modules are often used for:
- formatting helpers
- authentication logic
- validation helpers
- logging behavior
- reusable business rules
Why developers prefer modules
Instead of copying methods into many classes, developers define a module once and include it where needed.
Related pattern: guard against missing dependencies
If you try to include a module before loading it, your code fails early with a NameError. This is actually helpful because it tells you the dependency was not loaded.
Configuration and library usage
In larger Ruby apps, require is also used to load:
- gems
- framework components
- configuration files
- custom classes and modules
is not for loading files. It is only for mixing module methods into a class or module.
Common Mistakes
Mistake 1: Thinking require and include are interchangeable
They are not.
requireloads a fileincludeadds module methods to a class
Broken idea:
class Person
require 'greeting'
end
This may load a file, but it does not mix module methods into Person.
Mistake 2: Including a module before it is loaded
Broken code:
class Person
include Greeting
end
If Greeting has not been defined yet, Ruby raises an error.
Fix:
require_relative 'greeting'
class Person
include Greeting
Comparisons
| Concept | Purpose | Typical Use | Result |
|---|---|---|---|
require | Load code from a file | Gems, standard libraries, app files | Ruby knows about classes/modules in that file |
require_relative | Load a nearby file by relative path | Your own project files | Ruby loads the specified local file |
include | Mix a module into a class | Reuse instance methods | Instances gain module methods |
extend | Add module methods as class methods or singleton methods | Utility methods on a class/object | The class/object itself gains methods |
require vs
Cheat Sheet
Quick reference
Load code
require 'json'
require_relative 'my_module'
requireloads a library or gemrequire_relativeloads a local file by relative path- A file is generally loaded only once by
require
Mix in a module
module MyModule
def greet
"Hi"
end
end
class MyClass
include MyModule
end
includeadds module instance methods to class instances- It does not load the file where the module is defined
Common pattern
require_relative 'my_module'
class MyClass
include
FAQ
Do I need both require and include in Ruby?
Often, yes. If the module is in another file, you usually require or require_relative the file first, then include the module in your class.
What does require do in Ruby?
require loads and executes another file so its classes, modules, and code become available.
What does include do in Ruby?
include mixes a module's instance methods into a class so objects of that class can use them.
Can I use require instead of include?
No. require only loads the file. It does not add module methods to your class.
Can I use include without require?
Only if the module has already been loaded or defined earlier in the same program.
Should I use require or for my own files?
Mini Project
Description
Create a small Ruby program with a reusable module and a class that uses it. This demonstrates the real relationship between file loading and module inclusion: one file defines shared behavior, and another file loads that file and mixes the behavior into a class.
Goal
Build a class that gains reusable methods from a module stored in a separate file.
Requirements
Create a module in its own file with at least one instance method. Load that file from another Ruby file. Define a class that includes the module. Create an instance of the class and call the mixed-in method.
Keep learning
Related questions
Calling an Overridden Monkey-Patched Method in Ruby
Learn how to call the original method when monkey patching in Ruby, including alias_method patterns, examples, pitfalls, and practical usage.
Fixing Ruby Gem Native Extension Errors: mkmf.rb Can't Find Header Files for Ruby
Learn why Ruby gem installs fail with missing ruby.h, how native extensions work, and how to fix header file errors on Linux servers.
How to Add One Array to Another in Ruby Without Creating a Nested Array
Learn how to combine arrays in Ruby without getting nested arrays or nil values. Understand push, concat, +, and flatten clearly.