# Module methods

**URL:** <https://www.ruby-forum.com/t/module-methods/169418>\
**Category:** Ruby\
**Created:** [June 3, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/module-methods/169418 "2009-06-03T15:16:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 3, 2009, 3:16pm UTC](https://www.ruby-forum.com/t/module-methods/169418/1 "2009-06-03T15:16:53Z")

</div>

I am trying to do this:

==== File: good.rb ====

module Good  
def init\_module  
@var = “good”  
end

def life\_is\_good  
puts “life is #{@var}”  
end

[… more methods …]  
end

include X  
init\_module

======== and in a different file ========

include Good

life\_is\_good

* * *

That works but messes up when I try to use ruby-debug with it. So I  
changed it to try to call init\_module in a different way: instead of  
include X, init\_module, I tried both of these, neither of which works:

X::init\_module

X.init\_module

Each of those calls lead to an 'undefined method ‘init\_module’ for  
X:Module.

I am getting myself confused with Module methods etc etc.

Any help would be gratefully accepted!!

Pito

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 3, 2009, 3:32pm UTC](https://www.ruby-forum.com/t/module-methods/169418/2 "2009-06-03T15:32:07Z")

</div>

It looks like what you really want is a class. Is there a specific  
reason you don’t want to do this?

class Good  
def initialize  
@var = “good”  
end

def life  
puts @var  
end  
end

g = Good.new  
g.life

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 3, 2009, 3:53pm UTC](https://www.ruby-forum.com/t/module-methods/169418/3 "2009-06-03T15:53:12Z")

</div>

Mark T. wrote:

> It looks like what you really want is a class. Is there a specific  
> reason you don’t want to do this?
> 
> class Good  
> def initialize  
> @var = “good”  
> end
> 
> def life  
> puts @var  
> end  
> end
> 
> g = Good.new  
> g.life

Yeah, I am trying to make as transparent as possible “Domain Specific  
Language” so that the second file looks like a program in the DSL with  
as little extra stuff as possible. So the second file ends up looking  
like:

include Good

life  
bicycle :first  
telephone :last  
life

(FOR EXAMPLE 🙂

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 3, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/module-methods/169418/4 "2009-06-03T17:22:26Z")

</div>

Pretty sure that you need to prefix the module methods with the module  
name. Yeah, I know.

Try:  
module Good  
def Good.init\_module  
@var = “good”  
end  
…

And the in your “different file”

require ‘good’;  
Good.init\_module

Which isn’t exactly what you want.

I think that you have to prefix the method with the module name since using it this way gets things added to main rather than to your own class/instance. It smells like include should make it so that you can omit the Good. on the call, but it doesn't work for me. Again, I think it may be something to do with it being in main rather than in your own class. You're basically trying to make a mixin for main and I suspect/know that the rules are somewhat different.

Hope that this helps some…

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 3, 2009, 10:54pm UTC](https://www.ruby-forum.com/t/module-methods/169418/5 "2009-06-03T22:54:13Z")

</div>

On Wed, Jun 3, 2009 at 3:00 PM, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think part 4 of this article is worth reading:  
> [Dead Programmer Society: Ruby Domain Specific Languages - The Basics (Part 4)](http://deadprogrammersociety.blogspot.com/2006/11/ruby-domain-specific-languages-basics_27.html)

Also, the sample chapter of my book has a relevant section about this,  
under “Building Flexible Interfaces”:

[http://oreilly.com/catalog/9780596156749/index.html](http://oreilly.com/catalog/9780596156749/index.html)

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 3, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/module-methods/169418/6 "2009-06-03T21:01:04Z")

</div>

On Jun 3, 9:54 am, Pito S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> life  
> I think part 4 of this article is worth reading:  
> [Dead Programmer Society: Ruby Domain Specific Languages - The Basics (Part 4)](http://deadprogrammersociety.blogspot.com/2006/11/ruby-domain-specific-languages-basics_27.html)

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 9, 2009, 2:55pm UTC](https://www.ruby-forum.com/t/module-methods/169418/7 "2009-06-09T14:55:40Z")

</div>

On Mon, Jun 8, 2009 at 6:34 PM, Pito S.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Greg, I read the chapter. Excellent stuff. When’s the book coming out?  
> Or is it?

It’s already off to the printers so if you pre-order it now you should  
have it within the next couple weeks.

-greg

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 9, 2009, 12:34am UTC](https://www.ruby-forum.com/t/module-methods/169418/8 "2009-06-09T00:34:43Z")

</div>

Gregory B. wrote:

> On Wed, Jun 3, 2009 at 3:00 PM, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I think part 4 of this article is worth reading:  
> > [Dead Programmer Society: Ruby Domain Specific Languages - The Basics (Part 4)](http://deadprogrammersociety.blogspot.com/2006/11/ruby-domain-specific-languages-basics_27.html)
> 
> Also, the sample chapter of my book has a relevant section about this,  
> under “Building Flexible Interfaces”:
> 
> [http://oreilly.com/catalog/9780596156749/index.html](http://oreilly.com/catalog/9780596156749/index.html)

Greg, I read the chapter. Excellent stuff. When’s the book coming out?  
Or is it?

- Pito

---

<div class="post-metadata">

**Author:** ![pitosalas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a4c791/32.png) [@pitosalas](https://www.ruby-forum.com/u/pitosalas)\
**Post date:** [June 9, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/module-methods/169418/9 "2009-06-09T16:57:43Z")

</div>

Gregory B. wrote:

> On Mon, Jun 8, 2009 at 6:34 PM, Pito S.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Greg, I read the chapter. Excellent stuff. When’s the book coming out?  
> > Or is it?
> 
> It’s already off to the printers so if you pre-order it now you should  
> have it within the next couple weeks.
> 
> -greg

[http://www.salas.com/2009/06/09/geeky-check-out-ruby-best-practices-book-not-yet-out/](http://www.salas.com/2009/06/09/geeky-check-out-ruby-best-practices-book-not-yet-out/)

🙂
