# Class definition outside module

**URL:** <https://www.ruby-forum.com/t/class-definition-outside-module/192866>\
**Category:** Ruby\
**Created:** [August 7, 2010, 9:54am UTC](https://www.ruby-forum.com/t/class-definition-outside-module/192866 "2010-08-07T09:54:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raj\_plays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@raj\_plays](https://www.ruby-forum.com/u/raj_plays)\
**Post date:** [August 7, 2010, 9:54am UTC](https://www.ruby-forum.com/t/class-definition-outside-module/192866/1 "2010-08-07T09:54:40Z")

</div>

Hi,  
I have 2 classes, Parser and Util in parser.rb and util.rb respectively.  
Each has a lot of methods.  
I want to put them both inside one module but keep the class definition  
in separate files.  
So basically there will be 3 files now: mod.rb (that will have the  
module) and the 2 files mentioned above.

Is it possible?  
Please help

---

<div class="post-metadata">

**Author:** ![raj\_plays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@raj\_plays](https://www.ruby-forum.com/u/raj_plays)\
**Post date:** [August 7, 2010, 10:10am UTC](https://www.ruby-forum.com/t/class-definition-outside-module/192866/2 "2010-08-07T10:10:47Z")

</div>

On Saturday 07 August 2010, Rajarshi C. wrote:

> |Hi,  
> |I have 2  
> classes, Parser and Util in parser.rb and util.rb respectively.  
> |Each has  
> a lot of methods.  
> |I want to put them both inside one module but keep the  
> class definition  
> |in separate files.  
> |So basically there will be 3 files  
> now: mod.rb (that will have the  
> |module) and the 2 files mentioned  
> above.  
> |  
> |Is it possible?  
> |Please help

If I understand correctly what  
you mean, of course you can:

#mod.rb  
module Mod  
…  
end

#parser.rb  
module  
Mod

class Parser  
…  
end  
end

#util.rb  
module Mod  
class Util  
…

end  
end

Actually, you don’t need mod.rb, unless you want to put something  
in your module which isn’t related to any of the two classes.

I hope this  
helps

Stefano

---

<div class="post-metadata">

**Author:** ![raj\_plays](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@raj\_plays](https://www.ruby-forum.com/u/raj_plays)\
**Post date:** [August 7, 2010, 10:27am UTC](https://www.ruby-forum.com/t/class-definition-outside-module/192866/3 "2010-08-07T10:27:13Z")

</div>

Rajarshi C. wrote:

> Hi,  
> I have 2 classes, Parser and Util in parser.rb and util.rb respectively.  
> Each has a lot of methods.  
> I want to put them both inside one module but keep the class definition  
> in separate files.  
> So basically there will be 3 files now: mod.rb (that will have the  
> module) and the 2 files mentioned above.

Conventionally you would organise this as:

— lib/mod/parser.rb  
module Mod  
class Parser  
…  
end  
end

— lib/mod/util.rb  
module Mod  
class Util  
…  
end  
end

Then, if you wish, you can add:

— lib/mod.rb  
require ‘mod/parser’  
require ‘mod/util’
