From: Brian Candler Date: 2009-03-29T20:52:10+09:00 Subject: Re: where to declare project path for require Adam Akhtar wrote: > but in my case it complains saying "no > such file B.rb". IT should be able to find it because its in the same > diretory as A though. No - all files are searched for relative to the $LOAD_PATH ($:) entries only, not the directory which the file doing the requiring is in. Even if you have "." in $LOAD_PATH (meaning "current directory"), this refers to the process's current working directory, which is often not the same as the dir the script is in. I would do the following: [myproject/tests/test-helper.rb] # This sets up all the paths we need $:.unshift File.expand_path(File.dirname(__FILE__)+"/../lib") $:.unshift File.expand_path(File.dirname(__FILE__)) [myproject/tests/test.rb] require File.dirname(__FILE__)+"/test-helper" require 'samples/A' [myproject/tests/samples/A.rb] require 'samples/B' The test-helper.rb file sets up absolute paths in $LOAD_PATH for all the directories of interest. So the only thing that a top-level ruby app has to do (one which is started directly from the command line) is to require test-helper The example here assumes you have myproject/lib myproject/tests and adds these two directories to $LOAD_PATH. So if you do require "samples/A" then it will look for /path/to/myproject/tests/samples/A.rb and /path/to/myproject/lib/samples/A.rb Note that samples/A.rb does require 'samples/B', because $LOAD_PATH contains /path/to/myproject/tests (and not /path/to/myproject/tests/samples) By using absolute paths in $LOAD_PATH, it works regardless of which directory you are in when you invoke the script. e.g. # set current working directory to /tmp cd /tmp ruby /path/to/myproject/tests/test.rb Hope this is clear, Brian. -- Posted via http://www.ruby-forum.com/.