From: Abder-Rahman Ali on
I have this script with slight change from "The Little Book Of Ruby":

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.
--
Posted via http://www.ruby-forum.com/.

From: Martin DeMello on
On Wed, Jul 14, 2010 at 6:33 PM, Abder-Rahman Ali
<abder.rahman.ali(a)gmail.com> wrote:
> I have this script with slight change from "The Little Book Of Ruby":
>
> 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

From: Abder-Rahman Ali on
And, a small thing here. Can we say that "Modules" in Ruby are like
"Packages" in Java?

Thanks.
--
Posted via http://www.ruby-forum.com/.

From: Martin DeMello on
On Wed, Jul 14, 2010 at 6:39 PM, Abder-Rahman Ali
<abder.rahman.ali(a)gmail.com> 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://adhirajrankhambe.wordpress.com/2009/01/04/a-word-about-modules-in-ruby/

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

martin

From: Abder-Rahman Ali on
Thanks a lot Martin. Yes, "include" was the missing part.
--
Posted via http://www.ruby-forum.com/.