Good practices when writing modules


#1

Hi.

My learning-ruby-playground-project has gotten to the point where I
feel the need to split the source into several files. Coming from
python and java I’m both delighted and horrified to realize that file
names do not need to correspond to module names or class names.

I’d like to read something on good practices when writing ruby
modules. Are there any conventions that most writers adhere to? What’s
the equivalent of PYTHONPATH and JAVAHOME? What do I need to do in
order for my modules to be available without specifying the full path?
I’m looking for reading material dealing with these sort of questions.
Anything available online receives a bonus :wink:

TIA Alex


#2

Alex P. wrote:

order for my modules to be available without specifying the full path?
I’m looking for reading material dealing with these sort of questions.
Anything available online receives a bonus :wink:

TIA Alex

I don’t know of a specific resource, but I can help with a few things…

RUBYLIB - search path for programs
RUBYOPT - extra command line args

files are typically lower case with underscores, and when you require
you don’t use the ‘.rb’ suffix. The global variable $: is an array that
holds the current load path, and you can add to it at runtime if you
want to make other directories available for requiring. (Rather than
putting the whole path in for each require.)

Typically you will see this at the top of unit tests that are testing a
library with a typical directory structure: (root/test, root/lib)

$:.unshift(File.dirname(FILE) + ‘/…/lib’)

The unshift method just puts it at the front of the list, which I think
is done in case there might be a collision with something already
installed.

Hope this helps,

-Jeff