# Code organization and modules

**URL:** <https://www.ruby-forum.com/t/code-organization-and-modules/129297>\
**Category:** Ruby\
**Created:** [February 16, 2008, 8:49am UTC](https://www.ruby-forum.com/t/code-organization-and-modules/129297 "2008-02-16T08:49:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brad\_Ottoson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b2d939/32.png) [@Brad\_Ottoson](https://www.ruby-forum.com/u/Brad_Ottoson)\
**Post date:** [February 16, 2008, 8:49am UTC](https://www.ruby-forum.com/t/code-organization-and-modules/129297/1 "2008-02-16T08:49:19Z")

</div>

I’m working on an application for generating code skeletons from xml  
template files and have run into a snag. I’m trying to organize a set  
of code in a directory and inside that use modules. I have a set of  
files in a subdirectory of my project directory called lang\_cpp. I’ll  
paste in some code to show what I’m trying.

–method\_constructor.rb(in lang\_cpp directory):

module XTECpp::MethodConstructor  
def getDeclaration(codeClass)  
return " " + codeClass.name + “();\n”  
end  
end

–method\_destructor.rb(in lang\_cpp directory):  
module XTECpp::MethodDestructor  
def getDeclaration(codeClass)  
return " ~" + codeClass.name + “();\n”  
end  
end

Later in some other code I include the files using:  
require ‘lang\_cpp/method\_constructor.rb’  
require ‘lang\_cpp/method\_destructor.rb’

which seems to work fine. Later on I try the following:  
case funItem.name  
when “empty\_constructor”  
headerString \<\<  
XTECpp::MethodConstructor::getDeclaration(codeClass)  
when “empty\_destructor”  
headerString \<\<  
XTECpp::MethodDestructor::getDeclaration(codeClass)  
when “operator\_equality\_assignment”  
headerString \<\<  
XTECpp::MethodEqualityAssign::getDeclaration(codeClass)  
when “readugp”  
headerString \<\<  
XTECpp::MethodReadUGP::getDeclaration(codeClass)  
end

and when I run it I get the error:

C:\code\Ruby\XMLTemplateEngine\lib/lang\_cpp/method\_constructor.rb:5:  
uninitialized constant XTECpp (NameError)  
from C:\code\Ruby\XMLTemplateEngine\lib/lang\_cpp.rb:6:in  
`require' from C:\code\Ruby\XMLTemplateEngine\lib/lang_cpp.rb:6 from C:/code/Ruby/XMLTemplateEngine/lib/main.rb:6:in`require’  
from C:/code/Ruby/XMLTemplateEngine/lib/main.rb:6

I tried changing the module names to the form XTECpp\_MethodConstructor  
so each function was in it’s own unique module but I got errors doing  
that too. I’m not sure if you can ever do things this way in Ruby but  
I’d like to since this organization appeals to me.

---

<div class="post-metadata">

**Author:** ![Brad\_Ottoson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b2d939/32.png) [@Brad\_Ottoson](https://www.ruby-forum.com/u/Brad_Ottoson)\
**Post date:** [February 16, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/code-organization-and-modules/129297/2 "2008-02-16T13:30:50Z")

</div>

On 16.02.2008 08:48, Brad Ottoson wrote:

> ```
> return " " + codeClass.name + "();\n"
> 
> ```
> 
> Later in some other code I include the files using:  
> XTECpp::MethodDestructor::getDeclaration(codeClass)  
> C:\code\Ruby\XMLTemplateEngine\lib/lang\_cpp/method\_constructor.rb:5:  
> uninitialized constant XTECpp (NameError)  
> from C:\code\Ruby\XMLTemplateEngine\lib/lang\_cpp.rb:6:in `require' from C:\code\Ruby\XMLTemplateEngine\lib/lang_cpp.rb:6 from C:/code/Ruby/XMLTemplateEngine/lib/main.rb:6:in`require’  
> from C:/code/Ruby/XMLTemplateEngine/lib/main.rb:6
> 
> I tried changing the module names to the form XTECpp\_MethodConstructor  
> so each function was in it’s own unique module but I got errors doing  
> that too. I’m not sure if you can ever do things this way in Ruby but  
> I’d like to since this organization appeals to me.

You want to use modules as namespaces but actually you define instance  
methods in modules. A small change will provide what you want:

module XTECpp::MethodConstructor  
def self.getDeclaration(codeClass)  
return " " + codeClass.name + “();\n”  
end  
end

A few other remarks: if you have just one method per module then the  
idea with namespaces does not really make sense.

You use camelCase for method names while in Ruby code this\_style is  
conventionally used.

Your methods return leading whitespace which I assume serves to do some  
indentation (and also a trailing linefeed). I would not do that but let  
the invoking code make sure indentation is properly - otherwise you  
limit yourself to a single level of indentation. Also modularity of  
your code suffers because now your getXXX methods return a piece of code  
_and_ do indentation.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Brad\_Ottoson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b2d939/32.png) [@Brad\_Ottoson](https://www.ruby-forum.com/u/Brad_Ottoson)\
**Post date:** [February 16, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/code-organization-and-modules/129297/3 "2008-02-16T19:35:52Z")

</div>

Thanks for the fix and the advice. Using the self.getDeclaration fixed  
some problems. I also realized I needed to have the module XTECpp  
declared somewhere and that fixed the rest of the errors.
