f



include path for 'load' or 'require'

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

0
nospam21 (19047)
5/9/2005 10:08:35 PM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

1 Replies
367 Views

Similar Articles

[PageSpeed] 15

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__))


0
vjoel (2601)
5/9/2005 10:21:56 PM
Reply: