[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

include path for 'load' or 'require'

unknown

5/9/2005 10:09:00 PM

Hi! Say I have a script called "gen.rb", and I would like it
to load a script called "loadme.rb" because it provides a common
routine used by a lot of scripts, but I don't want to install
it anywhere in the search path... I just want to keep it with
all the .rb files that actually use it... How do I specify
the directory that 'this' current script is in?

For example, I have:
c:\src\gen.rb
c:\src\loadme.rb

where gen.rb is:
--------------------------------------
#!/usr/bin/env ruby
dir=File.dirname(__FILE__)
load "#{dir}/loadme.rb"
puts "__FILE__=#{__FILE__}, $0=#{$0}"
--------------------------------------

Then I try this:
c:\src\somewhere\else> ruby ..\..\gen.rb
__FILE__=../../gen.rb, $0=../../gen.rb

This works, but is there a better way to include a file that
exists in the same directory as the original script?

Thanks!
-- Glenn

1 Answer

Joel VanderWerf

5/9/2005 10:22:00 PM

0

Glenn M. Lewis wrote:
> Hi! Say I have a script called "gen.rb", and I would like it
> to load a script called "loadme.rb" because it provides a common
> routine used by a lot of scripts, but I don't want to install
> it anywhere in the search path... I just want to keep it with
> all the .rb files that actually use it... How do I specify
> the directory that 'this' current script is in?

You can add an entry to $LOAD_PATH, or $: (same thing), and it will
affect only the currently running script. But you still would have to
use File.dirname(__FILE__) . So, for example:

$LOAD_PATH.unshift(File.dirname(__FILE__))