From: Eero Saynatkari Date: 2006-08-14T00:49:41+09:00 Subject: 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/.