From: Chris Gehlker Date: 2006-08-12T23:23:21+09:00 Subject: Re: Modules -> do I have is straight? On Aug 12, 2006, at 6:06 AM, 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. Both modules and classes can become the ancestors of a class. The difference is that only one class may be the direct parent of a given class will the class can have any number of modules as its parent. A class designates another class as its parent with < and it designates modules as its parents with 'include'. The point of distinguishing between classes and modules may be hard to grasp until you deal with a language like C++ that allows multiple inheritance without making the distinction. In the C++ case you may have to resort to complicated rules to figure out what a given method call will do because the inheritance chain can be ambiguous. I'm sure many here can elaborate if you are curious about why distinguishing between classes and modules simplifies things. > 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? In Ruby there is no imposed correspondence between the namespace hierarchy and the file system. If you are writing and application for a furniture store and you want to distinguish between Database::Table and Inventory::Table you just do it. I was just using Why's Hpricot module. Now Hpricot is a weird word that is very unlikely to collide with anything in my code. Inside it are things like Hpricot::Element and Hpricot::Text and Hpricot::Traverse. These are all names that might well collide with my own names or names from other libraries if they weren't in a separate namespace. The organization of the files within Hpricot is along a different principle. many of the names are defined in more than one file. HTH -- The folly of mistaking a paradox for a discovery, a metaphor for a proof, a torrent of verbiage for a spring of capital truths, and oneself for an oracle, is inborn in us. -Paul Valery, poet and philosopher (1871-1945)