# Plug-in question: define a module first or just open the cla

**URL:** <https://www.ruby-forum.com/t/plug-in-question-define-a-module-first-or-just-open-the-cla/78089>\
**Category:** Rails\
**Created:** [December 3, 2006, 12:09am UTC](https://www.ruby-forum.com/t/plug-in-question-define-a-module-first-or-just-open-the-cla/78089 "2006-12-03T00:09:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 3, 2006, 12:09am UTC](https://www.ruby-forum.com/t/plug-in-question-define-a-module-first-or-just-open-the-cla/78089/1 "2006-12-03T00:09:06Z")

</div>

I notice that some plug-ins first do something like:  
module MyPlugin  
def do\_something\_very\_funky  
end  
end

and than include this into one of Rails’ existing classes (often in the  
same file).

module ActionController  
class Base  
include MyPlugin  
end  
end

Is there any advantage to this compared to simply doing:

module ActionController  
class Base  
def so\_something\_very\_funky  
end  
end  
end

I can’t think of any myself.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 3, 2006, 6:33am UTC](https://www.ruby-forum.com/t/plug-in-question-define-a-module-first-or-just-open-the-cla/78089/2 "2006-12-03T06:33:59Z")

</div>

Read the chapter on Classes and Modules in Programming Ruby by Dave  
Thomas. Then read the Rails Plugin - Extending the Rails Core  
Functionality by James A. for an indepth coverage on plugins.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 5, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/plug-in-question-define-a-module-first-or-just-open-the-cla/78089/3 "2006-12-05T19:04:16Z")

</div>

Well, I read that chapter before I started this thread 🙂

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 6, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/plug-in-question-define-a-module-first-or-just-open-the-cla/78089/4 "2006-12-06T19:07:26Z")

</div>

I understand which purpose modules serve. My question, however, was if  
there’s any  
advantage to defining a module for no other purpose than extending a  
particular class  
instead of just directly changing that class. I hope I didn’t add more  
confusion to my  
already vague question.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 6, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/plug-in-question-define-a-module-first-or-just-open-the-cla/78089/5 "2006-12-06T19:58:31Z")

</div>

unknown wrote:

> I understand which purpose modules serve. My question, however, was if  
> there’s any  
> advantage to defining a module for no other purpose than extending a  
> particular class  
> instead of just directly changing that class. I hope I didn’t add more  
> confusion to my  
> already vague question.

The end result is more or less the same. However, think of mix-ins as a  
way to extend any class with a set of related functionality. In this  
respect, a module included into existing classes makes more semantic  
sense.

If you adding a single utility method, simply opening the class would be  
fine. If you are adding a group of methods that work together toward a  
common functional goal, using a module is preferable since it really  
helps keep things organized and encapsulated.

Also a module name can be auto-documenting. While the goal of a bunch  
of added method to a base class might be unclear, it makes it easier to  
understand if the name of the module being included is something  
descriptive, such as ActsAsPuppy or GoogleMapsFinder.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 7, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/plug-in-question-define-a-module-first-or-just-open-the-cla/78089/6 "2006-12-07T17:59:52Z")

</div>

On Dec 6, 7:58 pm, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you adding a single utility method, simply opening the class would be  
> fine. If you are adding a group of methods that work together toward a  
> common functional goal, using a module is preferable since it really  
> helps keep things organized and encapsulated.
> 
> Also a module name can be auto-documenting. While the goal of a bunch  
> of added method to a base class might be unclear, it makes it easier to  
> understand if the name of the module being included is something  
> descriptive, such as ActsAsPuppy or GoogleMapsFinder.

Thank your very much for your clear description of the pros and cons of  
each  
approach. This was a much more detailed explanation than I could have  
ever  
hoped for. 😃

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 5, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/plug-in-question-define-a-module-first-or-just-open-the-cla/78089/7 "2006-12-05T20:16:26Z")

</div>

I don’t think you can use the same name ActionController for your  
module.  
The main idea here is that you once you define a module with your  
methods in  
it, you can mix-in any of the classes that you wish.

It gives you a multiple-inheritance like behaviour. It provides  
namespace  
also.
