Are there packages?

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?

Thanks.

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.

···

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?

--
Stefan

You need not put each class in a file. Normally you put each chunk of
logically connected code into on file, that may well be more than one
class, or in special cases also only half a class. Then you require
the files into the main file and start using the classes.

E.g.

main.rb:

···

On 22/07/05, EdUarDo <eduardo.yanezNOSPAM@nospamgmail.com> 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?

Thanks.

---
require 'ui_1.rb'
require 'ui_2.rb'

class Datastructure
  ...
end

class Program
  def initialize
    @ui1 = UI1.new(42)
    @ui2 = UI2.new("Hello world")
  end

  ...
end

Program.new
---

ui_1.rb:
---
class UI1
  ....
end
---

ui_2.rb:
---
class UI2
  ....
end
---

etc.

regards,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

Ruby has a variable called $LOAD_PATH which you can access/modify in...

What a wonderful answer, thank you!

You need not put each class in a file. Normally you put each chunk of
logically connected code into on file, that may well be more than one
class,

Well, that's a point I'd like to ask. Which is the philosophy to follow
with Ruby projects?. I used to Java and C++ projects where a class is defined
in a file (two for C++).
Is it better to group classes in a single file?

or in special cases also only half a class.

What is a half class?

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-l­inux

"Per default"... what if I install to another directory other than
/usr/local/lib? I'm having a problem now where I configured Ruby with
--prefix=/home/mydirectory and none of my require statements work
(trying to install rubygems).

I'm thinking there's either something seriously wrong with Ruby or I
need some real documentation. Where can I find out more (online) about
'require' and how it finds files? I've gone so far as to add $: =
'/home/mydirectory/lib/ruby/1.8' and other paths but 'require' still
acts stupid and won't find any of the files unless I add the specific
path to each and every file that ruby installed as part of its base
package. I find that a bit odd, and now that I have this problem I
find it odd that I can't find a good language reference online.

Help needed!

Thanks,

Patrick Price

EdUarDo <eduardo.yanezNOSPAM@NOSPAMgmail.com> writes:

You need not put each class in a file. Normally you put
each chunk of logically connected code into on file, that
may well be more than one class,

Well, that's a point I'd like to ask. Which is the
philosophy to follow with Ruby projects?. I used to Java
and C++ projects where a class is defined in a file (two
for C++). Is it better to group classes in a single file?

I'd say stick to one class per file in normal cases.
But know that Ruby, like C++ and unlike Java, doesn't care.

or in special cases also only half a class.

What is a half class?

Ruby, unlike both C++ and Java, allows you to reopen classes
after they have been defined. For example,

   $ cat a-1.rb
   class A
     def foo ; puts 123 end
   end

   $ cat a-2.rb
   class A
     def bar ; puts 456 end
   end

   $ cat main.rb
   require "a-1.rb"
   require "a-2.rb"
   a = A.new
   a.foo
   a.bar

You can do this with core Ruby classes too:

   class Array
     def rest ; self[1..-1] end
   end

   [1,2,3].rest #=> [2,3]

But like I said, just stick to one class per file (and one
file per class) unless you have a good reason not to.
That makes it easy to find any given class, and saves you
from having to think about which classes “belong together”.

···

--
Daniel Brockman <daniel@brockman.se>

    So really, we all have to ask ourselves:
    Am I waiting for RMS to do this? --TTN.

Something is wrong with your installation. I have six different
Ruby installations, five of them living somewhere under my
home directory and all work with their own libraries.
There is no magic behind require. All you need is ri:

    % ri require
--------------------------------------------------------- Kernel#require
     require(string) => true or false

···

On Sunday 24 July 2005 06:40, groups@jpprice.info wrote:

>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-l­inux

"Per default"... what if I install to another directory other than
/usr/local/lib? I'm having a problem now where I configured Ruby with
--prefix=/home/mydirectory and none of my require statements work
(trying to install rubygems).

I'm thinking there's either something seriously wrong with Ruby or I
need some real documentation. Where can I find out more (online) about
'require' and how it finds files?

------------------------------------------------------------------------
     Ruby tries to load the library named _string_, returning +true+ if
     successful. If the filename does not resolve to an absolute path,
     it will be searched for in the directories listed in +$:+. If the
     file has the extension ``.rb'', it is loaded as a source file; if
     the extension is ``.so'', ``.o'', or ``.dll'', or whatever the
     default shared library extension is on the current platform, Ruby
     loads the shared library as a Ruby extension. Otherwise, Ruby tries
     adding ``.rb'', ``.so'', and so on to the name. The name of the
     loaded feature is added to the array in +$"+. A feature will not be
     loaded if it's name already appears in +$"+. However, the file name
     is not converted to an absolute path, so that ``+require
     'a';require './a'+'' will load +a.rb+ twice.

        require "my-library.rb"
        require "db-driver"

First ensure that the files /home/mydirectory/bin/{ruby,irb,ri}
and directory /home/mydirectory/lib/ruby exist.
Perhaps you have an older ruby (executable) on PATH. Ensure that
PATH contains /home/mydirectory/bin.
Then run `ruby -e "puts $:"'. If the pathes printed do not start
with /home/mydirectory, then the wrong ruby gets executed.
Try: `type ruby' which shows you the path to ruby which gets
executed. If it's not /home/mydirectory/bin/ruby, e.g.
/usr/local/bin/ruby, rename it to, e.g. /usr/local/bin/old_ruby.

HTH,
  Stefan

But like I said, just stick to one class per file (and one
file per class) unless you have a good reason not to.
That makes it easy to find any given class, and saves you
from having to think about which classes "belong together".

Which is not neccessarily a good thing. Thinking about which classes
are coupled and which are not is an important design decision in my
opinion.

best regards,

Brian

···

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

This points are completely orthogonal to one another.

-austin

···

On 7/22/05, Brian Schröder <ruby.brian@gmail.com> wrote:

> But like I said, just stick to one class per file (and one
> file per class) unless you have a good reason not to.
> That makes it easy to find any given class, and saves you
> from having to think about which classes "belong together".
Which is not neccessarily a good thing. Thinking about which classes
are coupled and which are not is an important design decision in my
opinion.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca