# Code organization

**URL:** <https://www.ruby-forum.com/t/code-organization/169671>\
**Category:** Ruby\
**Created:** [June 6, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/code-organization/169671 "2009-06-06T18:07:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 6, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/code-organization/169671/1 "2009-06-06T18:07:11Z")

</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  
class B end  
class C end  
end

#######code 2#############  
class B  
include module A  
end

class C  
include module A  
end

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 6, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/code-organization/169671/2 "2009-06-06T18:54:03Z")

</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  
include module A  
end

class C  
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)

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 6, 2009, 8:00pm UTC](https://www.ruby-forum.com/t/code-organization/169671/3 "2009-06-06T20:00:10Z")

</div>

Brian C. 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

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 6, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/code-organization/169671/4 "2009-06-06T21:31:04Z")

</div>

Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> module A
> 
> class C  
> 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  
class B  
end  
end  
A::B.new

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

##### 2

module A  
def hello  
p “howdy”  
end  
end  
class B  
include A  
end  
B.new.hello

Module A’s instance methods are copied into class B as instance methods  
(“mixin”).

m.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [June 7, 2009, 7:36pm UTC](https://www.ruby-forum.com/t/code-organization/169671/5 "2009-06-07T19:36:45Z")

</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”
