On 1/11/07, ChrisH [email protected] wrote:
They could also add the path to the $LOAD_PATH, i.e:
$LOAD_PATH<<'‘folder_a/folder_b’
require ‘class_file’
This can create a problem if you have two files with the same name in
different directories.
I would recommend designing a fixed folder structure for your library,
rooted in one directory, such as:
lib
package1
class1.rb
class2.rb
package2
classA.rb
classB.rb
Each class that might depend on other classes makes a require
statement as if it was rooted in the lib directory. E.g. in
package1\class2.rb, have:
require ‘class1’ # Current directory always on load path
require ‘package2\classA’ # File in another directory requires path
To make that work, one file needs to be loaded before others which
adds the full path to the "lib" directory to the $LOAD_PATH variable
(AKA known as $:).
I recommend creating a small file that just has requires, which is
used to load your library from a given script. Example, imagine your
library lives under an app directory:
app
main.rb
library.rb
lib
package1
package2
etc.
library.rb would have these contents:
$: << “path to lib"
require 'package1\class1” # Load initial library files. Not
absolutely necessary.
require 'package2\classA" # Load initial library files. Not
absolutely necessary.
Then in main.rb, which is your app, you can just have this statement
to get your library loaded:
require ‘library’
Alternatively, you can make the modifications to $: in your main.rb
file, and then load the library. This skips the step of creating a
“library.rb” file, so main.rb just looks like:
$: << “path to lib"
require 'package1\class1”
require 'package2\classA"
Hope that helps.
Justin