# Modules in Ruby

**URL:** <https://www.ruby-forum.com/t/modules-in-ruby/191560>\
**Category:** Ruby\
**Created:** [July 14, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/modules-in-ruby/191560 "2010-07-14T15:03:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 3:03pm UTC](https://www.ruby-forum.com/t/modules-in-ruby/191560/1 "2010-07-14T15:03:16Z")

</div>

I have this script with slight change from “The Little Book Of Ruby”:

[http://pastie.org/private/6wgnmrtsodlmbh3ib1vg](http://pastie.org/private/6wgnmrtsodlmbh3ib1vg)

If I type the following: puts MyModule.greet

I get the required result.

But, there is an instance method inside the module “greet”. How can I  
call that method at the time modules CANNOT be instantiated?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 3:09pm UTC](https://www.ruby-forum.com/t/modules-in-ruby/191560/2 "2010-07-14T15:09:37Z")

</div>

And, a small thing here. Can we say that “Modules” in Ruby are like  
“Packages” in Java?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 3:10pm UTC](https://www.ruby-forum.com/t/modules-in-ruby/191560/3 "2010-07-14T15:10:08Z")

</div>

On Wed, Jul 14, 2010 at 6:33 PM, Abder-Rahman A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have this script with slight change from “The Little Book Of Ruby”:
> 
> [http://pastie.org/private/6wgnmrtsodlmbh3ib1vg](http://pastie.org/private/6wgnmrtsodlmbh3ib1vg)
> 
> If I type the following: puts MyModule.greet
> 
> I get the required result.
> 
> But, there is an instance method inside the module “greet”. How can I  
> call that method at the time modules CANNOT be instantiated?

Include it in a class

class MyClass  
include MyModule  
end

a = MyClass.new  
a.greet

martin

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 4:02pm UTC](https://www.ruby-forum.com/t/modules-in-ruby/191560/4 "2010-07-14T16:02:50Z")

</div>

On Wed, Jul 14, 2010 at 6:39 PM, Abder-Rahman A.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And, a small thing here. Can we say that “Modules” in Ruby are like  
> “Packages” in Java?

Modules in ruby serve a dual role as namespaces and mixins. You can  
think of a mixin as a Java interface, but one that can have concrete  
methods.

Check out the following:  
[http://ruby-doc.org/docs/ProgrammingRuby/html/tut\_modules.html](http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html)

> **[A Word about Modules in Ruby](https://adhirajrankhambe.wordpress.com/2009/01/04/a-word-about-modules-in-ruby/)**
>
> Not a word exactly, but I have a couple of things to share about Modules. Almost everyone who works with Ruby Or Ruby on Rails is aware of the advantages of Modules in Ruby. Just to summarize Modul…

and play with some code till it clicks. It’s a very useful part of the  
language.

martin

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 4:10pm UTC](https://www.ruby-forum.com/t/modules-in-ruby/191560/5 "2010-07-14T16:10:12Z")

</div>

Thanks a lot Martin. Yes, “include” was the missing part.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 4:45pm UTC](https://www.ruby-forum.com/t/modules-in-ruby/191560/6 "2010-07-14T16:45:19Z")

</div>

A small thing here:

When I type:

puts MyModule::GOODMOODE

In the purpose of accessing a module’s constant, I get the following:

uninitialized constant MyModule::GOODMOODE (NameError)

Although having the include.

How can I access a module’s constant.

And, when reading about modules, the resources I saw so far mention that  
modules includes:

1- Constants.  
2- Methods.  
3- Classes.

Don’t they include variables?!

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 14, 2010, 5:21pm UTC](https://www.ruby-forum.com/t/modules-in-ruby/191560/7 "2010-07-14T17:21:52Z")

</div>

In your private pastie, you have GOODMOOD, not GOODMOODE. I think this  
could be why. Accessing MyModule::GOODMOOD worked.

On Wed, Jul 14, 2010 at 09:45, Abder-Rahman A.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 21, 2010, 10:45am UTC](https://www.ruby-forum.com/t/modules-in-ruby/191560/8 "2010-07-21T10:45:04Z")

</div>

Steve Kim wrote:

> In your private pastie, you have GOODMOOD, not GOODMOODE. I think this  
> could be why. Accessing MyModule::GOODMOOD worked.
> 
> On Wed, Jul 14, 2010 at 09:45, Abder-Rahman A.

Thanks everyone for your replies.
