Modules

Hey, 
I just found out that you can also create Modules in Ruby. But I didn't quite understand what's the purpose of them. Can someone please help me how to use a module? 
 
Marvin

On Mon, Nov 25, 2013 at 4:15 PM, Marvin Johanning
[email protected] wrote:

I just found out that you can also create Modules in Ruby. But I didn’t
quite understand what’s the purpose of them. Can someone please help
me how to use a module?

There are many ways, but the main one I’ve seen is to “mix” it into
classes, to add or change some properties or behaviors on it. You can
mix it into a normal class, to give these new definitions to all
objects declared from it, or into a single object. You might do the
former if you want to declare, for instance, some new kind of
collection object, and give it all the awesome powers that Array,
Hash, and so on derive from Enumerable. You might do the latter if
you want to do DCI, so that for instance you could have a generic User
object under most circumstances but endow it with the powers from
Module Publisher just before it publishes a blog post.

-Dave

On Monday, 25 November 2013 at 04:39:57 pm -0500, Dave A. wrote:

objects declared from it, or into a single object. You might do the
former if you want to declare, for instance, some new kind of
collection object, and give it all the awesome powers that Array,
Hash, and so on derive from Enumerable. You might do the latter if
you want to do DCI, so that for instance you could have a generic User
object under most circumstances but endow it with the powers from
Module Publisher just before it publishes a blog post.

I think using module to effectively namespace the classes, modules,
etc., within is also a pretty common use. For example

module MyModule
class MyClass
end
end

MyClass # is an undefined class
MyModule::MyClass # is the intended class

Regards,
Henry

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs