Are there packages?

E

EdUarDo

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.
 
S

Stefan Lang

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
 
E

EdUarDo

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

What a wonderful answer, thank you!
 
B

Brian Schröder

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.
=20
How must I reference a class X from class Y if class X is in different di= rectory?
Is there a variable like CLASSPATH or anything else?
How have I to specify where are my classes?
=20
Thanks.
=20
=20

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:
---
require 'ui_1.rb'
require 'ui_2.rb'

class Datastructure
...
end

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

...
end

Program.new
---

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

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

etc.

regards,

Brian


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

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

EdUarDo

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?
 
D

Daniel Brockman

EdUarDo said:
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.
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 =3D 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 #=3D> [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 =E2=80=9Cbelong together=E2=80=9D=
 
B

Brian Schröder

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".
=20

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

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

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

Austin Ziegler

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.

This points are completely orthogonal to one another.

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
G

groups

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
 
S

Stefan Lang

"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=3D/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?

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
=2D-------------------------------------------------------- Kernel#require
require(string) =3D> true or false
=2D-----------------------------------------------------------------------
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"


=46irst 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,948
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top