# Code organization

**URL:** <https://rubytalk.org/t/code-organization/53775>\
**Category:** ruby-talk\
**Created:** [6 June 2009 16:07 UTC](https://rubytalk.org/t/code-organization/53775 "2009-06-06T16:07:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [6 June 2009 16:07 UTC](https://rubytalk.org/t/code-organization/53775/1 "2009-06-06T16:07:13Z")

</div>

Hi all,

What are the differences bewteen the following code organization?  
What situations are preferred by each one?

Thanks,

Li

#######code 1#############  
module A  
&nbsp;&nbsp;&nbsp;class B end  
&nbsp;&nbsp;&nbsp;class C end  
end

#######code 2#############  
class B  
&nbsp;&nbsp;&nbsp;include module A  
end

class C  
&nbsp;&nbsp;include module A  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [6 June 2009 16:54 UTC](https://rubytalk.org/t/code-organization/53775/2 "2009-06-06T16:54:21Z")

</div>

Li Chen wrote:

> What are the differences bewteen the following code organization?  
> What situations are preferred by each one?

In code 1, module A is just acting as a namespace, so that you have two  
classes, A::B and A::C. This is useful to ensure that your class names  
don't stomp on anyone else's use of the same names. All you have to do  
is ensure name A is unique.

Code 2 doesn't run as it stands, so you need to rewrite as:

module A; end

class B  
&nbsp;&nbsp;&nbsp;include module A  
end

class C  
&nbsp;&nbsp;include module A  
end

In this case, instance methods defined in module A are available in both  
class B and class C. This is almost the same as subclassing B and C from  
class A. B and C also get access to any constants defined in the A  
namespace, without needing the A:: prefix.

So in summary: they achieve two completely different ends (namespace  
separation, and method sharing, respectively)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Matt9](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Matt9](https://rubytalk.org/u/Matt9)\
**Post date:** [6 June 2009 19:30 UTC](https://rubytalk.org/t/code-organization/53775/3 "2009-06-06T19:30:07Z")

</div>

> Hi all,
> 
> What are the differences bewteen the following code organization?  
> What situations are preferred by each one?
> 
> Thanks,
> 
> Li
> 
> #######code 1#############  
> module A  
> &nbsp;&nbsp;&nbsp;class B end  
> &nbsp;&nbsp;&nbsp;class C end  
> end
> 
> #######code 2#############  
> class B  
> &nbsp;&nbsp;&nbsp;include module A  
> end
> 
> class C  
> &nbsp;&nbsp;include module A  
> end

They are totally different, and they illustrate the two main purposes of  
a module. Neither is legal as you have it so I will change the code to  
make it legal.

##### 1 ######

module A  
&nbsp;&nbsp;&nbsp;class B  
&nbsp;&nbsp;&nbsp;end  
end

A is a namespace, so you must say A::B from outside module A to refer to  
B.

##### 2 #####

module A  
&nbsp;&nbsp;&nbsp;def hello  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p "howdy"  
&nbsp;&nbsp;&nbsp;end  
end  
class B  
&nbsp;&nbsp;include A  
end  
B.new.hello

Module A's instance methods are copied into class B as instance methods  
("mixin").

m.

> **···**
>
> Li Chen \<chen\_li3@yahoo.com\> wrote:  
> A::B.new
> 
> --  
> matt neuburg, phd = matt@tidbits.com, [Matt Neuburg’s Home Page](http://www.tidbits.com/matt/)  
> Leopard - [http://www.takecontrolbooks.com/leopard-customizing.html](http://www.takecontrolbooks.com/leopard-customizing.html)  
> AppleScript - [http://www.amazon.com/gp/product/0596102119](http://www.amazon.com/gp/product/0596102119)  
> Read TidBITS! It's free and smart. [http://www.tidbits.com](http://www.tidbits.com)

---

<div class="post-metadata">

**Author:** ![Guest2](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@Guest2](https://rubytalk.org/u/Guest2)\
**Post date:** [6 June 2009 18:00 UTC](https://rubytalk.org/t/code-organization/53775/4 "2009-06-06T18:00:12Z")

</div>

Brian Candler wrote:

> So in summary: they achieve two completely different ends (namespace  
> separation, and method sharing, respectively)

Hi Brian,

Thank you so much.

As I recall correctly module has two major funtions/purposes:  
1) namespace separation 2) mixin. So you mean in the second code it  
works as a mixin or method sharing?

Li

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [7 June 2009 17:36 UTC](https://rubytalk.org/t/code-organization/53775/5 "2009-06-07T17:36:47Z")

</div>

Li Chen wrote:

> As I recall correctly module has two major funtions/purposes:  
> 1) namespace separation 2) mixin. So you mean in the second code it  
> works as a mixin or method sharing?

Yes, mixin or method sharing (two terms to describe the same thing)

Re-reading, there's another error I didn't fix in that example: it  
should say "include A" not "include module A"

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
