From: Charles Oliver Nutter Date: 2008-07-01T22:02:53+09:00 Subject: Re: Something like import/package in java? Christoffer Lernæ—¦ wrote: > Readability, clear division of responsibilities, ease of development. > > - Even with unit test suites I don't feel secure that the test isn't > conditional on files included by other tests. > - If I want to include all classes from a dir, I have to manually scan > through the dir. > - Clear cut organization of files. > - Crystal clear dependencies between files. > - Lots of other small worries I can't quite put my finger on. > > There's some breaking point around 20+ files for me (excluding unit > tests), when it becomes really important to have a well-defined > organization of my classes, and here just heaping classes into different > files doesn't quite cut it for me. Think of require in terms of "load this file" rather than "import everything defined in this file". All require does is an exactly-once execution of the file it finds to load, which will in most cases add methods, modules, or classes to the global namespace. To namespace your methods modules and classes, you want to embed them within other modules. The typical convention is that your x/y/z dir hierarchy will be mirrored in the z.rb file with a nested module X; module Y; class Z structure. Coming from Java, the first thing to remember is that Ruby does not impose (or gift, depending on your perspective) a mandatory file path == package structure, so you're free (or required) to do namespacing however you see fit. If you want a 50-deep dir hierarchy of .rb files to all dump stuff into the global namespace, you can do so. But you would typically apply your own namespacing for exactly the reasons you describe above. The closest rough equivalent to import would be include, which lets you pull in a whole module (as a namespace) into a given module or class (thereby making the former module's namespaced constants available without full qualification). - Charlie