Include vs Extend
Fast recall
Quick revision
Core answer
include SomeModule makes the module's instance methods available to instances of the receiving class, while extend SomeModule makes those methods singleton methods of the receiving object. Therefore User.include(Greeting) lets User.new.greet work; User.extend(Greeting) lets User.greet work. Any individual object can also be extended without changing other instances of its class.Whether you know include = instance methods and extend = class/singleton methods.
Using include when you wanted class methods (use extend), or expecting extend to affect instances.
include = instance methods (C.new has them); extend = class/singleton methods (C has them, or one obj). include for mixins; extend for class helpers.
Interview-ready guide
Interview answer
Both include and extend reuse instance methods from a module. The difference is who receives those methods. include Greeting inside User makes greet available to User instances, so the call is User.new.greet. User.extend(Greeting) adds it to the User class object, so the call is User.greet.
Use include when the behaviour belongs to every instance, such as each invoice being printable. Use extend on a class when the type itself should perform the operation, such as Invoice.from_csv(data). This is why Ruby modules can provide either instance behaviour or class-level helpers without inheritance.
The module below defines one ordinary instance method. Member includes it, while Team extends itself with it. The method body is the same, but self is whichever object receives the call.
Neither operation copies the module's own state into the receiver. The reused method runs with the receiver as self and can therefore use that receiver's instance variables. Extending one object, as in report.extend(DebugLabels), affects only that object. Keep a mixin focused on one clear capability so its required state is easy to understand.
Include vs Extend
Direct Answer
include SomeModule makes the module's instance methods available to instances of the receiving class, while extend SomeModule makes those methods singleton methods of the receiving object. Therefore User.include(Greeting) lets User.new.greet work; User.extend(Greeting) lets User.greet work. Any individual object can also be extended without changing other instances of its class.
Deep Dive & Architecture
A module stores instance-method definitions. include inserts that module into the ancestor chain used by instances of the class. Ruby still performs normal message lookup; it does not paste a second independent copy of each method into every object.
extend operates on one receiver. It includes the module in that receiver's singleton class, a hidden class that holds methods unique to the object. When the receiver happens to be a class object, those singleton methods are what Ruby developers normally call class methods.
This model explains why obj.extend(M) is local to obj and why module methods can read the receiver's state. Inside the reused method, self is the object that received the call—not the module where the method was written.
Hooks can combine both ideas, but a beginner-facing design often stays clearer with an explicit include InstanceMethods and extend ClassMethods. A module should express one coherent capability; using it only to avoid typing a few unrelated methods hides dependencies rather than improving design.
Code Example
module Greeting
def greet
"hello from #{self.class}"
end
end
class Member
include Greeting
end
Member.new.greet # => 'hello from Member'
class Team
extend Greeting
end
Team.greet # => 'hello from Class'
guest = Object.new
guest.extend(Greeting)
guest.greet # only guest gained this method
Common Mistakes & Pitfalls
Using include when you wanted class methods (use extend), or expecting extend to affect instances.
Check your understanding
Answer these without looking back at the article.
- Where does an included module appear in `Class#ancestors`?
- Why does extending a class create class-level methods?
- When is extending only one object useful, and when is it surprising?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.