# Module methods

**URL:** <https://rubytalk.org/t/module-methods/53712>\
**Category:** ruby-talk\
**Created:** [3 June 2009 13:16 UTC](https://rubytalk.org/t/module-methods/53712 "2009-06-03T13:16:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pito\_Salas](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Pito\_Salas](https://rubytalk.org/u/Pito_Salas)\
**Post date:** [3 June 2009 13:16 UTC](https://rubytalk.org/t/module-methods/53712/1 "2009-06-03T13:16:56Z")

</div>

I am trying to do this:

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

module Good  
&nbsp;&nbsp;&nbsp;def init\_module  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@var = "good"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def life\_is\_good  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "life is #{@var}"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;[... 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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mark\_Thomas](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark\_Thomas](https://rubytalk.org/u/Mark_Thomas)\
**Post date:** [3 June 2009 13:30 UTC](https://rubytalk.org/t/module-methods/53712/2 "2009-06-03T13:30:05Z")

</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  
&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@var = "good"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def life  
&nbsp;&nbsp;&nbsp;&nbsp;puts @var  
&nbsp;&nbsp;end  
end

g = Good.new  
g.life

---

<div class="post-metadata">

**Author:** ![Nation\_Carey](https://avatars.discourse-cdn.com/v4/letter/n/cc9497/32.png) [@Nation\_Carey](https://rubytalk.org/u/Nation_Carey)\
**Post date:** [3 June 2009 15:21 UTC](https://rubytalk.org/t/module-methods/53712/3 "2009-06-03T15:21:39Z")

</div>

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

Try:  
module Good  
&nbsp;&nbsp;&nbsp;def Good.init\_module  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@var = "good"  
&nbsp;&nbsp;&nbsp;end  
....

And the in your "different file"

require 'good';  
Good.init\_module

Which isn't exactly what you want.

\<talking out of ear now\>  
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.  
\</talking out of ear now\>

Hope that this helps some...

> **···**
>
> -----Original Message-----  
> From: [rps@salas.com](mailto:rps@salas.com) [[mailto:rps@salas.com](mailto:rps@salas.com)]  
> Sent: Wednesday, June 03, 2009 9:17 AM  
> To: ruby-talk ML  
> Subject: Module methods
> 
> I am trying to do this:
> 
> ==== File: good.rb ====
> 
> module Good  
> &nbsp;&nbsp;&nbsp;def init\_module  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@var = "good"  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;def life\_is\_good  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "life is #{@var}"  
> &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;[... 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  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Pito\_Salas](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Pito\_Salas](https://rubytalk.org/u/Pito_Salas)\
**Post date:** [3 June 2009 13:54 UTC](https://rubytalk.org/t/module-methods/53712/4 "2009-06-03T13:54:28Z")

</div>

Mark Thomas 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  
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;&nbsp;&nbsp;@var = "good"  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def life  
> &nbsp;&nbsp;&nbsp;&nbsp;puts @var  
> &nbsp;&nbsp;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 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mark\_Thomas](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@Mark\_Thomas](https://rubytalk.org/u/Mark_Thomas)\
**Post date:** [3 June 2009 19:00 UTC](https://rubytalk.org/t/module-methods/53712/5 "2009-06-03T19:00:09Z")

</div>

I think part 4 of this article is worth reading:

> **[Ruby Domain Specific Languages - The Basics (Part 4)](http://deadprogrammersociety.blogspot.com/2006/11/ruby-domain-specific-languages-basics_27.html)**
>
> In the previous three entries in this series of postings, I have been exploring the basics of creating domain specific languages using Ruby...

> **···**
>
> On Jun 3, 9:54 am, Pito Salas \<r...@salas.com\> wrote:
> 
> > 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

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [3 June 2009 20:53 UTC](https://rubytalk.org/t/module-methods/53712/6 "2009-06-03T20:53:12Z")

</div>

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)

> **···**
>
> On Wed, Jun 3, 2009 at 3:00 PM, Mark Thomas \<mark@thomaszone.com\> 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)

---

<div class="post-metadata">

**Author:** ![Pito\_Salas](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Pito\_Salas](https://rubytalk.org/u/Pito_Salas)\
**Post date:** [8 June 2009 22:34 UTC](https://rubytalk.org/t/module-methods/53712/7 "2009-06-08T22:34:46Z")

</div>

Gregory Brown wrote:

> **···**
>
> > On Wed, Jun 3, 2009 at 3:00 PM, Mark Thomas \<mark@thomaszone.com\> 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  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [9 June 2009 12:54 UTC](https://rubytalk.org/t/module-methods/53712/8 "2009-06-09T12:54:01Z")

</div>

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

-greg

> **···**
>
> On Mon, Jun 8, 2009 at 6:34 PM, Pito Salas\<rps@salas.com\> wrote:
> 
> > Gregory Brown wrote:
> > 
> > > On Wed, Jun 3, 2009 at 3:00 PM, Mark Thomas \<mark@thomaszone.com\> 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?

---

<div class="post-metadata">

**Author:** ![Pito\_Salas](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Pito\_Salas](https://rubytalk.org/u/Pito_Salas)\
**Post date:** [9 June 2009 14:57 UTC](https://rubytalk.org/t/module-methods/53712/9 "2009-06-09T14:57:50Z")

</div>

Gregory Brown wrote:

> **···**
>
> > On Mon, Jun 8, 2009 at 6:34 PM, Pito Salas\<rps@salas.com\> 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/)
> 
> 🙂  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
