# Module Definition Idioms

**URL:** <https://rubytalk.org/t/module-definition-idioms/28951>\
**Category:** ruby-talk\
**Created:** [14 July 2006 01:18 UTC](https://rubytalk.org/t/module-definition-idioms/28951 "2006-07-14T01:18:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Armstrong](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eric\_Armstrong](https://rubytalk.org/u/Eric_Armstrong)\
**Post date:** [14 July 2006 01:18 UTC](https://rubytalk.org/t/module-definition-idioms/28951/1 "2006-07-14T01:18:00Z")

</div>

Looking at HTree, I see a few more things  
that seem weird to me, but which are  
probably perfectly normal Ruby:

file: parse.rb

> **···**
>
> --------------  
> module HTree  
> &nbsp;&nbsp;&nbsp;def HTree.parse  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;&nbsp;def
> 
> &nbsp;&nbsp;&nbsp;def HTree.parse\_xml  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;...
> 
> &nbsp;&nbsp;&nbsp;def Text.parse\_cdata\_section(raw\_string)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;def Comment.parse(raw\_string)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;&nbsp;end
> 
> end # module  
> ---------------
> 
> This is all very strange and new to me.  
> There are a dozen files, each of which adds  
> to the "HTree" module, and in each module,  
> methods are added to several classes.
> 
> What makes that a desirable way to structure  
> things? In the Java world, each class is  
> in its own file. What are the advantages of  
> doing things this way, instead?

---

<div class="post-metadata">

**Author:** ![Rob\_Sanheim1](https://avatars.discourse-cdn.com/v4/letter/r/7ea924/32.png) [@Rob\_Sanheim1](https://rubytalk.org/u/Rob_Sanheim1)\
**Post date:** [14 July 2006 04:23 UTC](https://rubytalk.org/t/module-definition-idioms/28951/2 "2006-07-14T04:23:22Z")

</div>

Probably to define the module HTree as the top level namespace.  
"Text" and "Comment" are pretty common names for classes/modules, so  
putting them inside an HTree module puts them in their own namespace  
and keeps everything organized.

- Rob

> **···**
>
> On 7/13/06, Eric Armstrong \<Eric.Armstrong@sun.com\> wrote:
> 
> > Looking at HTree, I see a few more things  
> > that seem weird to me, but which are  
> > probably perfectly normal Ruby:
> > 
> > file: parse.rb  
> > --------------  
> > module HTree  
> > &nbsp;&nbsp;&nbsp;def HTree.parse  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;&nbsp;def
> > 
> > &nbsp;&nbsp;&nbsp;def HTree.parse\_xml  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;...
> > 
> > &nbsp;&nbsp;&nbsp;def Text.parse\_cdata\_section(raw\_string)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;def Comment.parse(raw\_string)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > end # module  
> > ---------------
> > 
> > This is all very strange and new to me.  
> > There are a dozen files, each of which adds  
> > to the "HTree" module, and in each module,  
> > methods are added to several classes.
> > 
> > What makes that a desirable way to structure  
> > things? In the Java world, each class is  
> > in its own file. What are the advantages of  
> > doing things this way, instead?
> 
> --
> 
> > **[Home](https://robsanheim.com/)**
> >
> > Just another WordPress site
> 
> > **[Stock Market Analysis & Tools for Investors](https://seekingalpha.com/)**
> >
> > Join Seeking Alpha, the largest investing community in the world. Get stock market news and analysis, investing ideas, earnings calls, charts and portfolio analysis tools.
> 
> > **[Purchase Intent Data for Enterprise Tech Sales and Marketing | TechTarget](https://www.techtarget.com/)**
> >
> > Identify, influence and engage active buyers in your tech market with TechTarget's purchase intent insight-powered solutions. Activity matters.

---

<div class="post-metadata">

**Author:** ![Eric\_Armstrong](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eric\_Armstrong](https://rubytalk.org/u/Eric_Armstrong)\
**Post date:** [14 July 2006 06:51 UTC](https://rubytalk.org/t/module-definition-idioms/28951/3 "2006-07-14T06:51:32Z")

</div>

Rob Sanheim wrote:

> **···**
>
> > On 7/13/06, Eric Armstrong \<Eric.Armstrong@sun.com\> wrote:
> > 
> > > file: parse.rb  
> > > --------------  
> > > module HTree  
> > > &nbsp;&nbsp;&nbsp;def HTree.parse  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > > &nbsp;&nbsp;&nbsp;def
> > > 
> > > &nbsp;&nbsp;&nbsp;def Comment.parse(raw\_string)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > > &nbsp;&nbsp;&nbsp;end
> > > 
> > > end # module  
> > > ---------------
> > > 
> > > What makes that a desirable way to structure  
> > > things?
> > 
> > Probably to define the module HTree as the top level namespace.  
> > "Text" and "Comment" are pretty common names for classes/modules, so  
> > putting them inside an HTree module puts them in their own namespace  
> > and keeps everything organized.
> 
> Aha. Module as namespace. That rings a bell.  
> Thanks.

---

<div class="post-metadata">

**Author:** ![Eric\_Armstrong](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eric\_Armstrong](https://rubytalk.org/u/Eric_Armstrong)\
**Post date:** [14 July 2006 06:57 UTC](https://rubytalk.org/t/module-definition-idioms/28951/4 "2006-07-14T06:57:48Z")

</div>

Ok. I get that using a module lets you establish  
a namespace. Now for the other part of the question:

&nbsp;&nbsp;&nbsp;Why is the HTree module split up among half  
&nbsp;&nbsp;&nbsp;a dozen files: parse.rb, and others??

And come to think of it, is it possible that  
the Comment and HTree classes are intuited by  
Ruby, since they're not explicitly defined,  
in this file at least?

Rob Sanheim wrote:

> **···**
>
> > On 7/13/06, Eric Armstrong \<Eric.Armstrong@sun.com\> wrote:
> > 
> > > file: parse.rb  
> > > --------------  
> > > module HTree  
> > > &nbsp;&nbsp;&nbsp;def HTree.parse  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > > &nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;...  
> > > &nbsp;&nbsp;&nbsp;def Comment.parse(raw\_string)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > > &nbsp;&nbsp;&nbsp;end
> > > 
> > > end # module  
> > > ---------------
> > > 
> > > What makes that a desirable way to structure  
> > > things?
> > 
> > Probably to define the module HTree as the top level namespace.
> 
> \>

---

<div class="post-metadata">

**Author:** ![Matthew\_Smillie](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Matthew\_Smillie](https://rubytalk.org/u/Matthew_Smillie)\
**Post date:** [14 July 2006 08:46 UTC](https://rubytalk.org/t/module-definition-idioms/28951/5 "2006-07-14T08:46:06Z")

</div>

> Ok. I get that using a module lets you establish  
> a namespace. Now for the other part of the question:
> 
> &nbsp;&nbsp;Why is the HTree module split up among half  
> &nbsp;&nbsp;a dozen files: parse.rb, and others??

This question sounds to me an awful lot like "why organise your code at all? why not just use one big file?" For large projects (or even small-to-medium projects that you expect other people to use), some sort of functional organisation is necessary.

> And come to think of it, is it possible that  
> the Comment and HTree classes are intuited by  
> Ruby, since they're not explicitly defined,  
> in this file at least?

I don't believe so. Class names are constants, and as such have to be initialized somewhere before they're they're used. Try this example:

module Foo  
&nbsp;&nbsp;&nbsp;def Bar.bar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "hello world"  
&nbsp;&nbsp;&nbsp;end  
end

-:2: uninitialized constant Foo::Bar (NameError)

HTree::Comment and HTree::Text will be defined somewhere in one of the files 'require'd into parse.rb (this includes the whole chain of a-requires-b-requires-c-etc).

I'll throw in my bit on the original question as well:

Rob Sanheim wrote:

> > [HTree parse.rb example]  
> > What makes that a desirable way to structure  
> > things?
> 
> Probably to define the module HTree as the top level namespace.

In this case it also seems to encapsulate similar behaviour within a single file: all the parsing methods for the various classes are defined in a single location. More generally, if Java is "one class, one file," this approach would be "one behaviour, one file."

> In the Java world, each class is  
> in its own file. What are the advantages of  
> doing things this way, instead?

Quite similar to the Java model, I'd say, just substitute 'behaviour' for 'class' in all the points. For example, if, in Java, you'd say that an error would be easy to localise because you know where the class is defined, here you might say it's easy to localise because you know where that behaviour is defined.

Which one is more appropriate likely depends both on the code and the coder. I imagine that for certain class structures where there are many similarities in behaviour across the classes(e.g., there's an A#a, B#a, C#a, D#a, etc. that all do similar things), but little interaction within classes (e.g., A#a and A#b don't particularly care about each other), grouping by behaviour makes a lot of sense, at least to me.

matthew smillie.

> **···**
>
> On Jul 14, 2006, at 7:57, Eric Armstrong wrote:
> 
> > On 7/13/06, Eric Armstrong \<Eric.Armstrong@sun.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Eric\_Armstrong](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@Eric\_Armstrong](https://rubytalk.org/u/Eric_Armstrong)\
**Post date:** [14 July 2006 23:38 UTC](https://rubytalk.org/t/module-definition-idioms/28951/6 "2006-07-14T23:38:19Z")

</div>

Matthew Smillie wrote:

> > Ok. I get that using a module lets you establish  
> > a namespace. Now for the other part of the question:
> > 
> > &nbsp;&nbsp;Why is the HTree module split up among half  
> > &nbsp;&nbsp;a dozen files: parse.rb, and others??
> 
> This question sounds to me an awful lot like "why organise your code at all? why not just use one big file?" For large projects (or even small-to-medium projects that you expect other people to use), some sort of functional organisation is necessary.

You are kidding, right? I am just not grokking this  
particular style of organization. Everything I've  
seen about modules explains the syntax. But the  
syntax is being used in ways I never expected.

So far, I've seen:  
&nbsp;&nbsp;&nbsp;\* classes inside of modules (expected)  
&nbsp;&nbsp;&nbsp;\* modules inside of classes (why?)  
&nbsp;&nbsp;&nbsp;\* big files with multiple modules in them (why?)  
&nbsp;&nbsp;&nbsp;\* modules split up among multiple files,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adding methods to different classes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(explained below. Thanks!)

Understanding the syntax of "module" and "class"  
doesn't begin to help understand the philosophy  
behind those organizational styles, or help me  
understand the mental model that produced them.

So I'm here, hoping to expand my comprehension.

> Class names are constants, and as such have to be initialized somewhere before they're they're used.

\> Try this example:

> module Foo  
> &nbsp;&nbsp;def Bar.bar  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "hello world"  
> &nbsp;&nbsp;end  
> end
> 
> -:2: uninitialized constant Foo::Bar (NameError)

Right. I should have tried that experiment.

> HTree::Comment and HTree::Text will be defined somewhere in one of the files 'require'd into parse.rb (this includes the whole chain of a-requires-b-requires-c-etc).

Makes sense.

> I'll throw in my bit on the original question as well:
> 
> Rob Sanheim wrote:
> 
> > > [HTree parse.rb example]  
> > > What makes that a desirable way to structure  
> > > things?
> > 
> > Probably to define the module HTree as the top level namespace.
> 
> In this case it also seems to encapsulate similar behaviour within a single file: all the parsing methods for the various classes are defined in a single location. More generally, if Java is "one class, one file," this approach would be "one behaviour, one file."

AHA! That is a /really/ interesting observation.  
That seems useful, somehow. I like it immediately.

> > In the Java world, each class is  
> > in its own file. What are the advantages of  
> > doing things this way, instead?
> 
> Quite similar to the Java model, I'd say, just substitute 'behaviour' for 'class' in all the points. For example, if, in Java, you'd say that an error would be easy to localize because you know where the class is defined, here you might say it's easy to localise because you know where that behaviour is defined.
> 
> Which one is more appropriate likely depends both on the code and the coder. I imagine that for certain class structures where there are many similarities in behaviour across the classes(e.g., there's an A#a, B#a, C#a, D#a, etc. that all do similar things), but little interaction within classes (e.g., A#a and A#b don't particularly care about each other), grouping by behaviour makes a lot of sense, at least to me.

Thanks much. Grouping by behavior is a very interesting  
concept. When I learn something that changes one bit of  
parsing code, there is a good chance that I will need to  
modify other bits, as well. That organization could make  
it easier to replicate changes.

So that covers

> **···**
>
> > On Jul 14, 2006, at 7:57, Eric Armstrong wrote:
> > 
> > > On 7/13/06, Eric Armstrong \<Eric.Armstrong@sun.com\> wrote:
