Re: Modules -> do I have is straight?



Carl Jenkins wrote:
I was hoping I could expand on the thread a little bit...

I sort of understand modules, according to what I have read in my fancy
ruby book and this forum modules are like superclasses. But, what about
this < ?
Doesn't this allow a class to "become" a subclass of another by way of
inheritance. Not sure I am catching the difference between the two.

Modules should really not be used like superclasses--although
you are correct, an #included module does become an ancestor
of the #including class and method lookup follows this. The
commonly used name for the module behaviour is 'mixin'.

I personally prefer modules for mainly two cases:

1) 'Intersecting' behaviour, something that is added to multiple
classes as an enhancement but does not really make the classes
'related' enough for an inheritance relationship.

2) Variable composition--a class can be customised/enabled/disabled
for certain tasks by adding/withholding/changing its module
composition. This can, of course, also be achieved by either
actual composition by variables or then by subclassing.

Also, I am not quite sure I understand how namespaces works in ruby.
In java you create a package ( i know you all know this already I am
just saying it for my benefit) to place related classes in. Say for
example com.jenkins.util.MyFancyClass. If I want to use this class in
another package com.jenkins.library any classs in library would just
import it. How does this work in ruby?
Most of the code I have seen is in the same class or at least the same
direcctory. Is it typical for ruby applications to create a package
structure similar to a java app?

The namespace, unlike in Java, is in no way enforced and it
cannot be used to resolve a file's location in the filesystem.
It is strictly a convenience to give the code some structure
and to insulate it from namespace clashes.

It is certainly possible to put your code in a similar namespace
to what you would use in Java:

module Com
module Company
module ProjectName
module LibraryName
class SomeClass
# ...
end
end
end
end
end

Typically, though, only the ProjectName/LibraryName downwards
is used.

I think the most common file layout is the one required by
install.rb/setup.rb (see http://raa.ruby-lang.org) or, more
recently, RubyGems.

Thanks for your time!


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

.



Relevant Pages

  • Re: why cl packages are hard to use ?
    ... with are interned by the reader, and most non-advanced Lisp programmers ... qualify our symbols with the name of some package, ... applied very effectively and modularly to any namespace problem. ... To contrast with the situation in Ruby, ...
    (comp.lang.lisp)
  • Re: Some understanding questions...
    ... > must be private? ... class that has same namespace or not. ... In Java you should be able to do so ... Since there's the notion of "package private" too as I ...
    (microsoft.public.dotnet.vjsharp)
  • Re: Package == NameSpace?
    ... In Java the directory structure should match the package structure. ... Java has default/package accessibility. ...
    (comp.lang.java.programmer)
  • Re: Modules -> do I have is straight?
    ... I've read most of the Pickaxe book and most of Why's guide 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, ... 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. ...
    (comp.lang.ruby)
  • require an entire package?
    ... I'm about three weeks into Ruby coming from C++ and Java and enjoying ... other) package for each given class to do its job. ... "Is there a way to require multiple items from ...
    (comp.lang.ruby)