Justin Collins
8/9/2006 7:25:00 PM
Jean Nibee wrote:
> Hi
>
> First off, thanks for letting me be a Ruby noob. I've read most of the
> Pickaxe book and most of Why's guide (whee it's in PDF now) and I am
> getting comfortable w/ Ruby. Coming from a long Java background I tend
> to try and find similarities of things in Ruby to things in Java, simply
> to try and 'get' them better.
>
> Modules have messed me up a bit but here's what I understand. (I'm the
> only Ruby guy at my office so I have no-one to throw these questions off
> of).
>
> Modules in effect 'wrap functionality', they have no other purpose than
> to package up code that can be used/imported/inlined into other code and
> classes. Since you can't instantiate them, they do nothing by
> themselves. They are part of a greater whole.
>
As Arnaud mentioned, they can 'do' stuff on their own if you create
module-level methods. Remember, modules are objects, too.
> That being said.. could they be seen (From a functional standpoint) as
> an abstract class? In other words, they are a piece of 'incomplete'
> functionality that we must implement / use and build on but we can't use
> by themselves.
>
Actually, many people like to use them as namespaces as well. Packaging
up functionality, as you say. Trying to make sure method names don't
conflict with other packages.
> Thanks for your time.
>
>
> P.S. Do you package Modules in a different place than actual classes? (I
> still live like a java programmer and put all my classes in their own
> file.)
>
There's nothing wrong with putting each class it its own file, and you
can do the same with modules. In fact, you can even spread
classes/modules across multiple files, since classes are always 'open'.
Or put multiple classes/modules in one file. There is no enforced manner
of doing things.
Hope that helps a little.
-Justin