# What does "extend self" do?

**URL:** <https://www.ruby-forum.com/t/what-does-extend-self-do/81237>\
**Category:** Ruby\
**Created:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237 "2007-01-06T19:30:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237/1 "2007-01-06T19:30:33Z")

</div>

I happened to be reading dependencies.rb in the Rails source, and it  
starts like this:

require ‘set’  
require File.dirname( **FILE** ) + ‘/core\_ext/module/attribute\_accessors’  
require File.dirname( **FILE** ) + ‘/core\_ext/load\_error’  
require File.dirname( **FILE** ) + ‘/core\_ext/kernel’

module Dependencies #:nodoc:  
extend self  
…

What is the “extend self” doing? I thought at the top a module, ‘self’  
was pretty much an empty context at that point… but I guess not,  
since the writer obviously thinks self contains something worth  
extending…?

Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237/2 "2007-01-06T19:30:41Z")

</div>

On Jan 4, 2007, at 9:09 PM, Jeff wrote:

> extend self  
> …
> 
> What is the “extend self” doing? I thought at the top a module,  
> ‘self’  
> was pretty much an empty context at that point… but I guess not,  
> since the writer obviously thinks self contains something worth  
> extending…?

It extends the very module object. That’s one-liner to add all the  
module instance methods as module functions. That is

module Foo  
extend self  
def foo  
‘foo’  
end  
end

allows the call Foo.foo.

To do it by hand you’d add a

module\_function :method

for each method.

– fxn

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237/3 "2007-01-06T19:30:42Z")

</div>

Alle 21:09, giovedì 4 gennaio 2007, Jeff ha scritto:

> …
> 
> What is the “extend self” doing? I thought at the top a module, ‘self’  
> was pretty much an empty context at that point… but I guess not,  
> since the writer obviously thinks self contains something worth  
> extending…?
> 
> Jeff

In a module definition, outside methods, self refers to the module  
itself, as  
it happens inside the definition of a class. For instance, the following  
code

module MyModule  
puts self.class  
puts self.name  
end

gives

Module  
MyModule

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [January 6, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237/4 "2007-01-06T19:31:00Z")

</div>

On Jan 4, 2007, at 2:09 PM, Jeff wrote:

> extend self  
> …
> 
> What is the “extend self” doing? I thought at the top a module,  
> ‘self’  
> was pretty much an empty context at that point… but I guess not,  
> since the writer obviously thinks self contains something worth  
> extending…?

Well, like any method call in Ruby, there is a receiver here. In  
this case, it’s just the implicit self, so the call is actually  
self.extend(self). Self, in that context, is the module  
Dependancies. Dependancies.extend(Dependancies) means, duplicate all  
the instance methods as module methods.

Does that make sense?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237/5 "2007-01-19T16:29:34Z")

</div>

Google seems to have lost my previous 2 attempts to reply to this  
thread, here we go again…

Awesome, thanks for the help everyone! For some reason when I saw the  
“extend” at the top of the class, instead of at the bottom, I didn’t  
see how it know about the instance methods that follow. Doh!

Thanks again,  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237/6 "2007-01-19T16:30:05Z")

</div>

Xavier N. wrote:

> > 
> 
> allows the call Foo.foo.
> 
> To do it by hand you’d add a
> 
> module\_function :method
> 
> for each method.

Not quite the same however. Using module\_function actually creates a  
new method that is a copy of the first. extend\_self OTOH adds the  
module to it’s own metaclass’ inheritance chain, so in that case they  
are the same method.

T.

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [January 6, 2007, 7:30pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237/7 "2007-01-06T19:30:47Z")

</div>

Hi –

On Fri, 5 Jan 2007, Jeff wrote:

> …
> 
> What is the “extend self” doing? I thought at the top a module, ‘self’  
> was pretty much an empty context at that point… but I guess not,  
> since the writer obviously thinks self contains something worth  
> extending…?

self is never empty; it’s always something. At the top of a module  
definition, it’s the module object itself:

module M  
p self  
end

will print:

M

So what extend self does is it extends the module object by making the  
instance methods in the module available to it:

module M  
def greet  
puts “hi”  
end  
extend self  
end

M.greet # =\> hi

Now the object M has access to the instance methods defined in M –  
which it also happens to _be_ 🙂

David

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237/8 "2007-01-19T16:31:59Z")

</div>

On Jan 4, 2007, at 10:05 PM, Trans wrote:

> > To do it by hand you’d add a
> > 
> > module\_function :method
> > 
> > for each method.
> 
> Not quite the same however. Using module\_function actually creates a  
> new method that is a copy of the first. extend\_self OTOH adds the  
> module to it’s own metaclass’ inheritance chain, so in that case they  
> are the same method.

Right, I just meant them to be conceptually similar to add some  
redundancy to the explanation. But it wasn’t exact.

Thank you!

– fxn

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/what-does-extend-self-do/81237/9 "2007-01-19T16:30:16Z")

</div>

removed\_email\_address@domain.invalid wrote:

> So what extend self does is it extends the module object by making the  
> instance methods in the module available to it:

(and to all the others who said similar things)

Thanks everyone! For some reason I thought that order was important,  
and having it appear before defining the instance methods would be  
futile. As soon as I started reading all the replies I realized I was  
being silly.

Thanks again.  
Jeff
