From: Stefan Lang Date: 2005-07-22T19:01:52+09:00 Subject: Re: Are there packages? On Friday 22 July 2005 11:10, EdUarDo wrote: > Hi, I'm new to Ruby. I'm going to start a new project and I'd like > to know how could organize the classes. > I want to make a directory structure that allows me to have the classes > ordered and organized, like I'd do with C or Java. > > How must I reference a class X from class Y if class X is in different > directory? Is there a variable like CLASSPATH or anything else? > How have I to specify where are my classes? Ruby has a variable called $LOAD_PATH which you can access/modify in scripts. This variable is an array of directories in which Ruby looks for scripts if you call require. Per default the following pathes are in $LOAD_PATH: /usr/local/lib/ruby/site_ruby/1.8 /usr/local/lib/ruby/site_ruby/1.8/i686-linux /usr/local/lib/ruby/site_ruby /usr/local/lib/ruby/1.8 /usr/local/lib/ruby/1.8/i686-linux . (slightly different if you aren't on Linux) Now, if you call require in a script: require 'fileutils' Ruby searches in the directories in $LOAD_PATH to find a file called fileutils.rb and executes the first it can find. Usually, Ruby projects use the following directory structure: bin/ # contains Ruby scripts which will be installed my_program.rb my_program2.rb lib/ # contains scripts which contain classes/modules # they'll be installed into /usr/local/lib/ruby/site_ruby/1.8 my_program/ foo.rb # contains, e.g. class MyProgram::Foo app.rb # contains, e.g. class MyProgram::App test/ # contains test scripts using Test::Unit test_foo.rb test_bar.rb bin/my_program.rb could contain code like the following: require 'my_program/app' MyProgram::App.new.run The install scripts install.rb and setup.rb (avaiable on RAA) know this directory structure and know how to install the files under lib and bin. Just copy setup.rb into the projects directory and run: % ruby setup.rb and then you can run my_program.rb and my_program2.rb from the commandline: % my_program.rb Note that calling "require" doesn't do anything special with regards to namespaces, it just searches for the given file and executes it as normal ruby code. You could as well write all the code for a project into a single file. -- Stefan