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

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 Mon, Nov 25, 2013 at 4:15 PM, Marvin Johanning <marvin-johanning@web.de> 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?

--
Dave Aronson, the T. Rex of Codosaurus LLC (codosaur.us),
freelance software developer, and creator of these sites:
PullRequestRoulette.com, blog.codosaur.us, & Dare2XL.com.

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

···

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

On Mon, Nov 25, 2013 at 4:15 PM, Marvin Johanning > <marvin-johanning@web.de> 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.