# Extend self?

**URL:** <https://www.ruby-forum.com/t/extend-self/95305>\
**Category:** Ruby\
**Created:** [May 27, 2006, 9:11am UTC](https://www.ruby-forum.com/t/extend-self/95305 "2006-05-27T09:11:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluntsword](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f1d935/32.png) [@bluntsword](https://www.ruby-forum.com/u/bluntsword)\
**Post date:** [May 27, 2006, 9:11am UTC](https://www.ruby-forum.com/t/extend-self/95305/1 "2006-05-27T09:11:24Z")

</div>

I am a newbie to Ruby. I’ve found some code in the file dependencies.rb  
within Active Support:

module Dependencies  
extend self  
…

I would like to ask what does ‘extend self’ do here? What’s purpose? I  
didn’t found any document mentioning such ‘self-extension’ case.

Cheers,  
Donald

---

<div class="post-metadata">

**Author:** ![bluntsword](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f1d935/32.png) [@bluntsword](https://www.ruby-forum.com/u/bluntsword)\
**Post date:** [May 27, 2006, 9:33am UTC](https://www.ruby-forum.com/t/extend-self/95305/2 "2006-05-27T09:33:44Z")

</div>

That’s quite simple, it saves you some typing 🙂

module A  
def a  
p ‘a’  
end  
end

A.a # doesn’t work, because it is not written als def self.a - you  
have to include the module in a class and initialize the class to  
access it.

module A  
extend self  
def a  
p ‘a’  
end  
end

A.a

# =\> ‘a’

voila, so now we can access the methods directly, otherwise you would  
have to write

module A  
def self.a  
p ‘a’  
end  
def a  
p ‘a’  
end  
end

or similar… makes things a bit simpler - oh, and this is no  
scientific point of view of course, just wait for the multitude of  
responses that will explain it in detail

( i count on you guys 🙂

---

<div class="post-metadata">

**Author:** ![bluntsword](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f1d935/32.png) [@bluntsword](https://www.ruby-forum.com/u/bluntsword)\
**Post date:** [May 27, 2006, 10:06am UTC](https://www.ruby-forum.com/t/extend-self/95305/3 "2006-05-27T10:06:41Z")

</div>

Really thanks to the quick response, but it seems that the two ways of  
definitions are not really equivalent. Just did an experiment.

If I say

module A  
def self.a  
p ‘a’  
end  
end

class C  
include A  
end

> C.a  
> NoMethodError: undefined method `a’ for C:Class

> c = C.new  
> c.a  
> NoMethodError: undefined method `a’ for #\<C:0x7e478\>

While if I changed the definition of module A to

module A  
extend self  
def a  
p ‘a’  
end  
end

…

> C.a  
> NoMethodError: undefined method `a’ for C:Class  
> c = C.new  
> c.a  
> “a”

Anyone can explain this?

Cheers,  
Donald

---

<div class="post-metadata">

**Author:** ![bluntsword](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f1d935/32.png) [@bluntsword](https://www.ruby-forum.com/u/bluntsword)\
**Post date:** [May 27, 2006, 12:11pm UTC](https://www.ruby-forum.com/t/extend-self/95305/4 "2006-05-27T12:11:21Z")

</div>

Donald Luo wrote:

> > C.a  
> > NoMethodError: undefined method `a’ for C:Class
> 
> Anyone can explain this?

Module functions are not transfered by include by default. You could do  
this:

class C  
include A  
extend A  
end
