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 Person can call hello.
Why this matters
In real Ruby programs, code is usually split across many files. That means:
- You often
requirea file so Ruby knows about the module. - Then you
includethe module so your class gets its methods.
Example:
require_relative 'greeting'
class Person
include Greeting
end
Here:
require_relative 'greeting'loads the file.include Greetingmixes the module methods into the class.
These are not alternatives. They do different kinds of work.
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 hello
"Hello"
end
end
And another file named person.rb:
require_relative 'greeting'
class Person
include Greeting
end
person = Person.new
puts person.hello
What this does
require_relative 'greeting'loads the file whereGreetingis defined.include GreetinggivesPersoninstances access tohello.
If you skip require_relative, Ruby may raise an error because Greeting is not known yet.
If you skip include, the module exists, but Person does not get its methods.
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
class Person
include Greeting
end
Ruby creates the Person class.
4. include Greeting mixes the module into the class
Now instances of Person can use methods from Greeting.
5. A new object is created
person = Person.new
This makes one Person instance.
6. Ruby calls the mixed-in method
puts person.hello
Ruby looks for hello on the person object:
- Check methods defined directly on
Person - Check included modules
- Find
helloinGreeting - Return
"Hello"
Output:
Hello
What if the module is in another file?
Then Ruby must load that file first:
require_relative 'greeting'
class Person
include Greeting
end
Without loading the file, Greeting may be undefined.
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
include Trackable
end
This is common in Ruby applications that organize code into many files.
4. Rails concerns and shared modules
Rails applications often define reusable modules for shared logic, then include them in models or controllers.
module Sluggable
def slug
name.downcase.gsub(' ', '-')
end
end
A model can include that module to gain the behavior.
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
include 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
end
Mistake 3: Expecting include to add class methods
include adds instance methods, not class methods.
module Greeting
def hello
"Hello"
end
end
class Person
include Greeting
end
Person.hello
This fails because hello is available on instances:
Person.new.hello
Mistake 4: Forgetting require_relative for your own files
For local files, beginners often write:
require 'greeting'
Depending on load paths, this may not work as expected. For files in the same project, require_relative is often clearer:
require_relative 'greeting'
Mistake 5: Defining methods in a module but never including it
Broken code:
module Greeting
def hello
"Hello"
end
end
class Person
end
puts Person.new.hello
Fix:
class Person
include Greeting
end
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 include
require_relative 'greeting' # load file
include Greeting # use module methods in instances
include vs extend
module Greeting
def hello
"Hello"
end
end
class Person
include Greeting
end
Person.new.hello # works
class Person
extend Greeting
end
Person.hello # works
Use include when instances should get the methods.
Use extend when the class itself should get the methods.
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 MyModule
end
Remember
require= load codeinclude= reuse module methods in instances- Often you need both
includeis not a replacement forrequirerequireis not a replacement forinclude
Related rule
If you want class methods instead of instance methods, look at extend.
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 require_relative for my own files?
For local project files, require_relative is usually the clearer choice because it uses a path relative to the current file.
Does include add class methods?
No. include adds instance methods. For class methods, extend is commonly used.
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
Add a Default Value to an Existing Column in a Rails Migration
Learn how to add or change a default value on an existing database column using Rails Active Record migrations safely.
Calling a Class Method from an Instance in Ruby
Learn how to call a class method from an instance in Ruby using self.class, with examples, pitfalls, and practical usage patterns.
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.