# Self.included question

**URL:** <https://rubytalk.org/t/self-included-question/37645>\
**Category:** ruby-talk\
**Created:** [16 May 2007 20:02 UTC](https://rubytalk.org/t/self-included-question/37645 "2007-05-16T20:02:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_Hoefler](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Dave\_Hoefler](https://rubytalk.org/u/Dave_Hoefler)\
**Post date:** [16 May 2007 20:02 UTC](https://rubytalk.org/t/self-included-question/37645/1 "2007-05-16T20:02:24Z")

</div>

Hi there,

I see the following code chunk in a lot of Rails plugins:

&nbsp;&nbsp;def self.included(base)  
&nbsp;&nbsp;&nbsp;&nbsp;base.extend(ClassMethods)  
&nbsp;&nbsp;end

&nbsp;&nbsp;module ClassMethods  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def method\_one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def method\_two  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

I'm most curious about "self.included". I checked out the docs on 'included'  
([http://www.ruby-doc.org/core/classes/Module.html#M001661](http://www.ruby-doc.org/core/classes/Module.html#M001661)). They pointed to  
'Module.append\_features' (  
[http://www.ruby-doc.org/core/classes/Module.html#M001659](http://www.ruby-doc.org/core/classes/Module.html#M001659)). Even after  
reading the docs, I'm still a little lost. Could someone break it down a  
little more than the docs? ...or point me at some other resource. I'd like  
to know "why" this is done.

Thanks,  
Dave

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [16 May 2007 20:19 UTC](https://rubytalk.org/t/self-included-question/37645/2 "2007-05-16T20:19:11Z")

</div>

> Hi there,
> 
> I see the following code chunk in a lot of Rails plugins:
> 
> &nbsp;&nbsp;def self.included(base)  
> &nbsp;&nbsp;&nbsp;&nbsp;base.extend(ClassMethods)  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;module ClassMethods  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def method\_one  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def method\_two  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> I'm most curious about "self.included". I checked out the docs on 'included'  
> ([class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001661%5C)).

This is the doc about #include not #included!!

> They pointed to  
> 'Module.append\_features' (  
> [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001659%5C)). Even after  
> reading the docs, I'm still a little lost. Could someone break it down a  
> little more than the docs? ...or point me at some other resource. I'd like  
> to know "why" this is done.
> 
> Thanks,  
> Dave

The included hook is run whenever a module is included.  
The parameter base passed to the hook is referring to the class into  
which the module is included.  
Basically  
def self.included(base)  
&nbsp;&nbsp;&nbsp;base.extend(ClassMethods)  
end  
means:  
and if you include me you also extend your class to all methods  
defined in ClassMethods.

This means that instead of writing

class X  
&nbsp;&nbsp;&nbsp;include Y  
&nbsp;&nbsp;&nbsp;extend ClassMethods  
end

you write  
class X  
&nbsp;&nbsp;&nbsp;include Y # ==\> the hook does X.extend(ClassMethods)  
end

Although I do not know Rails it might be a reasonable guess that the  
functionality of ClassMethods is closely coupled with the  
functionality of the inserted module - and that methods of the module  
need class methods from ClassMethods.

HTH  
Robert

> **···**
>
> On 5/16/07, Dave Hoefler \<dhoefler@gmail.com\> wrote:
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [16 May 2007 20:23 UTC](https://rubytalk.org/t/self-included-question/37645/3 "2007-05-16T20:23:26Z")

</div>

If a module implements self.included the interpreter calls that method whenever the module is included in another module or class, which is passed as its single argument.

Object#extend on the other hand adds the instance methods of the module or modules passed as arguments to the target object (as singleton methods). If the object is a class, that means you are making module instance methods available as class methods in that class, and that's what base is when a module is being included in a class.

In short, plugins put methods that will end up being class methods (acts\_as\_something, say) in their own nested module, conventionally called ClassMethods, and then include them in init.rb. For instance

&nbsp;&nbsp;&nbsp;ActiveRecord::Base.send(:include, MyModule)

That triggers inclusion when Rails boots, so your code is able to call those class methods as if they belonged to the original API of ActiveRecord::Base (in the example).

-- fxn

> **···**
>
> On May 16, 2007, at 10:02 PM, Dave Hoefler wrote:
> 
> > Hi there,
> > 
> > I see the following code chunk in a lot of Rails plugins:
> > 
> > def self.included(base)  
> > &nbsp;&nbsp;&nbsp;base.extend(ClassMethods)  
> > end
> > 
> > module ClassMethods  
> > &nbsp;&nbsp;&nbsp;&nbsp;def method\_one  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;def method\_two  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > I'm most curious about "self.included". I checked out the docs on 'included'  
> > ([class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001661%5C)). They pointed to  
> > 'Module.append\_features' (  
> > [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001659%5C)). Even after  
> > reading the docs, I'm still a little lost. Could someone break it down a  
> > little more than the docs? ...or point me at some other resource. I'd like  
> > to know "why" this is done.

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Don1](https://avatars.discourse-cdn.com/v4/letter/w/958977/32.png) [@Wolfgang\_Nadasi-Don1](https://rubytalk.org/u/Wolfgang_Nadasi-Don1)\
**Post date:** [16 May 2007 20:35 UTC](https://rubytalk.org/t/self-included-question/37645/4 "2007-05-16T20:35:09Z")

</div>

Robert Dober schrieb:

> > I'm most curious about "self.included". I checked out the docs on 'included'  
> > ([class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001661%5C)).
> 
> This is the doc about #include not #included!!

You are looking for the wrong Method - see documentation about "Object#extend". The usages are visible in the example:

module ClassMethods  
&nbsp;&nbsp;&nbsp;def method\_one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "in method\_one"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def method\_two  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "in method\_two"  
&nbsp;&nbsp;&nbsp;end  
end

def incl1(x)  
&nbsp;&nbsp;&nbsp;x.extend(ClassMethods)  
end

module Mymod  
&nbsp;&nbsp;&nbsp;def self.incl2(x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x.extend(ClassMethods)  
&nbsp;&nbsp;&nbsp;end  
end

incl1(String)  
Mymod.incl2(Array)

String.method\_one # =\> in method\_one  
String.method\_two # =\> in method\_two

Array.method\_one # =\> in method\_one

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![Dave\_Hoefler](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Dave\_Hoefler](https://rubytalk.org/u/Dave_Hoefler)\
**Post date:** [16 May 2007 20:39 UTC](https://rubytalk.org/t/self-included-question/37645/5 "2007-05-16T20:39:35Z")

</div>

Robert and Xavier (fxn) -

Thank you for the explanations! That just cleared things up tremendously.

So say I have this module (rails plugin):

module Office  
&nbsp;&nbsp;def self.included(base)  
&nbsp;&nbsp;&nbsp;&nbsp;base.extend(ClassMethods)  
&nbsp;&nbsp;end

&nbsp;&nbsp;module ClassMethods  
&nbsp;&nbsp;&nbsp;&nbsp;def working(options={})  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something based off the options  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

end

In one of my models I can then access the method 'working' by:

class ModelName \< ActiveRecord::Base  
&nbsp;&nbsp;&nbsp;working :task =\> "Take a break!"  
end

Thanks again

-Dave

> **···**
>
> On 5/16/07, Xavier Noria \<fxn@hashref.com\> wrote:
> 
> > On May 16, 2007, at 10:02 PM, Dave Hoefler wrote:
> > 
> > \> Hi there,  
> > \>  
> > \> I see the following code chunk in a lot of Rails plugins:  
> > \>  
> > \> def self.included(base)  
> > \> base.extend(ClassMethods)  
> > \> end  
> > \>  
> > \> module ClassMethods  
> > \> def method\_one  
> > \> end  
> > \>  
> > \> def method\_two  
> > \> end  
> > \> end  
> > \>  
> > \> I'm most curious about "self.included". I checked out the docs on  
> > \> 'included'  
> > \> ([http://www.ruby-doc.org/core/classes/Module.html#M001661\](http://www.ruby-doc.org/core/classes/Module.html#M001661%5C)). They  
> > \> pointed to  
> > \> 'Module.append\_features' (  
> > \> [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001659%5C)). Even after  
> > \> reading the docs, I'm still a little lost. Could someone break it  
> > \> down a  
> > \> little more than the docs? ...or point me at some other resource.  
> > \> I'd like  
> > \> to know "why" this is done.
> > 
> > If a module implements self.included the interpreter calls that  
> > method whenever the module is included in another module or class,  
> > which is passed as its single argument.
> > 
> > Object#extend on the other hand adds the instance methods of the  
> > module or modules passed as arguments to the target object (as  
> > singleton methods). If the object is a class, that means you are  
> > making module instance methods available as class methods in that  
> > class, and that's what base is when a module is being included in a  
> > class.
> > 
> > In short, plugins put methods that will end up being class methods  
> > (acts\_as\_something, say) in their own nested module, conventionally  
> > called ClassMethods, and then include them in init.rb. For instance
> > 
> > &nbsp;&nbsp;&nbsp;ActiveRecord::Base.send(:include, MyModule)
> > 
> > That triggers inclusion when Rails boots, so your code is able to  
> > call those class methods as if they belonged to the original API of  
> > ActiveRecord::Base (in the example).
> > 
> > -- fxn

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [16 May 2007 20:42 UTC](https://rubytalk.org/t/self-included-question/37645/6 "2007-05-16T20:42:39Z")

</div>

> Robert Dober schrieb:

No it was not me, it was OP!

> \>\> I'm most curious about "self.included". I checked out the docs on  
> \>\> 'included'  
> \>\> ([class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001661%5C)).  
> \> This is the doc about #include not #included!!

\<snip\>  
Robert

> **···**
>
> On 5/16/07, Wolfgang Nádasi-Donner \<wonado@donnerweb.de\> wrote:
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [16 May 2007 20:46 UTC](https://rubytalk.org/t/self-included-question/37645/7 "2007-05-16T20:46:00Z")

</div>

\<snip\>

> module Office  
> &nbsp;&nbsp;def self.included(base)  
> &nbsp;&nbsp;&nbsp;&nbsp;base.extend(ClassMethods)  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;module ClassMethods  
> &nbsp;&nbsp;&nbsp;&nbsp;def working(options={})  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something based off the options  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> end
> 
> In one of my models I can then access the method 'working' by:
> 
> class ModelName \< ActiveRecord::Base

#given that you include Office before  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include Office

> &nbsp;&nbsp;&nbsp;working :task =\> "Take a break!"  
> end
> 
> Thanks again
> 
> -Dave

Cheers  
Robert

> **···**
>
> On 5/16/07, Dave Hoefler \<dhoefler@gmail.com\> wrote:
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [16 May 2007 21:05 UTC](https://rubytalk.org/t/self-included-question/37645/8 "2007-05-16T21:05:03Z")

</div>

Exactly.

In regular Ruby you'd need to include Office by hand in the model class, but in Rails it is normal (I'd say even expected) that the plugin does the include in AR::Base for you in init.rb using the idiom mentioned in the previous post.

-- fxn

> **···**
>
> On May 16, 2007, at 10:39 PM, Dave Hoefler wrote:
> 
> > Robert and Xavier (fxn) -
> > 
> > Thank you for the explanations! That just cleared things up tremendously.
> > 
> > So say I have this module (rails plugin):
> > 
> > module Office  
> > def self.included(base)  
> > &nbsp;&nbsp;&nbsp;base.extend(ClassMethods)  
> > end
> > 
> > module ClassMethods  
> > &nbsp;&nbsp;&nbsp;def working(options={})  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# do something based off the options  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > end
> > 
> > In one of my models I can then access the method 'working' by:
> > 
> > class ModelName \< ActiveRecord::Base  
> > &nbsp;&nbsp;working :task =\> "Take a break!"  
> > end
