Require

Guys,

When we use

require ‘some_code’.

where (i.e., in which directories) does require looks for ‘some_code’?

Thanks,
Maurício

require 'some_code'.

where (i.e., in which directories) does require looks for 'some_code'?

$LOAD_PATH

pigeon% ruby -e 'p $LOAD_PATH'
["/usr/local/lib/ruby/site_ruby/1.6",
"/usr/local/lib/ruby/site_ruby/1.6/i686-linux",
"/usr/local/lib/ruby/site_ruby", "/usr/local/lib/ruby/1.6",
"/usr/local/lib/ruby/1.6/i686-linux", "."]
pigeon%

Guy Decoux

Hi –

···

On Fri, 13 Dec 2002, [ISO-8859-1] Maurício wrote:

Guys,

When we use

require ‘some_code’.

where (i.e., in which directories) does require looks for ‘some_code’?

Short answer: those listed in the global variable $:

Longer answer:

candle:~$ ri require
-------------------------------------------------------- Kernel::require
require( aString ) → true or false

 Ruby tries to load the library named aString, 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 already appears in $". require returns true if the
 feature was successfully loaded.
    require "my-library.rb"
    require "db-driver"

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

···

On Fri, 13 Dec 2002, ts wrote:

require ‘some_code’.

where (i.e., in which directories) does require looks for ‘some_code’?

$LOAD_PATH

Whoops, I gave the Perlish version :slight_smile:

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Interesting… The good-style way of changing it is just
$LOAD_PATH.push ‘directory’ ?

ts wrote:

···

“M” == =?ISO-8859-1?Q?Maur=EDcio?= writes:

require ‘some_code’.

where (i.e., in which directories) does require looks for ‘some_code’?

$LOAD_PATH

(…)

Hi –

···

On Fri, 13 Dec 2002, [ISO-8859-1] Maurício wrote:

Interesting… The good-style way of changing it is just
$LOAD_PATH.push ‘directory’ ?

Depending on the situation, you might want to shift instead of
pushing, so that the directories you’re adding get searched first.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav