0%

Include vs Extend in Ruby

In this article, we will study Include and Extend and see the differences between those two. Include is for adding methods to an instance of a class and extend is for adding methods to any type of object(Class or an instance of Class). Let’s take a look at a small example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
module Foo
def bar
puts 'foobar'
end
end

class IncludeModule
include Foo
end

class ExtendModule
extend Foo
end

class Plain; end

on IncludeModule:

1
2
IncludeModule.bar # => undefined method `bar' for IncludeModule:Class (NoMethodError)
IncludeModule.new.bar # => 'foobar'

As you can see, include a module inside a class will add the instance methods defined in the module to the instances of the class including the module.

on ExtendModule:

1
2
ExtendModule.bar # => 'foobar'
ExtendModule.new.bar # => undefined method `bar' for #<ExtendModule:0x007f848197aee8> (NoMethodError)

Extend a module inside a class will add the instance methods defined in the module to the extended class.

on an instance of Plain Class:

1
2
3
4
5
6
7
plain = Plain.new
plain.bar # => undefined method `bar' for #<Plain:0x007fb252086f90> (NoMethodError)
plain.extend(Foo)
plain.bar # => 'foobar'

new_plain = Plain.new
new_plain.bar # => undefined method `bar' for #<Plain:0x007fb252086f90> (NoMethodError)

Extend a module on an object will add the instance methods defined in the module to the object, and the module will not available in other objects.

Footnotes