Library path relative to current .rb file

Z

Zoran Lazarevic

One of the most irritating (missing) features of Ruby is inability to
'require' files in the same directory or subdirectories as the
executing source file. In other programming languages (Java, C, C++)
that is commonly used. Note that this is different from the current
woriking directory.

I do not want to add the directory of the currenty executing file to
the library path because that would apply to all the other files,
including standard library (e.g. I usually have 'utils.rb' in every
project, and that would make a confusion if I tried to use two
projects).

$LOAD_PATH << File.dirname($0) # VERY BAD - does not work for
libs
$LOAD_PATH << File.dirname(__FILE__) # Also VERY BAD - huge
LOAD_PATH

The solution is to prepend current file's directory before every
'require', which is very ugly.

require File.join(File.dirname(__FILE__), 'utils' ) # UGLY

My question is:
- Why aren't required files looked up in subdirectories relative to
the current file?
- Is there an easy way to simulate that in Ruby?
- Can it be easily fixed in Ruby interpreter source code?

Thanks,

--Laza
 
G

Gavin Sinclair

One of the most irritating (missing) features of Ruby is inability to
'require' files in the same directory or subdirectories as the
executing source file. In other programming languages (Java, C, C++)
that is commonly used. Note that this is different from the current
woriking directory.
Agreed.

The solution is to prepend current file's directory before every
'require', which is very ugly.
require File.join(File.dirname(__FILE__), 'utils' ) # UGLY

It's a lot less ugly if you separate the ugly stuff into a variable
first.

require 'pathname' # 1.8

this_dir = Pathname.new(File.dirname(__FILE__))

require this_dir + "utils.rb"

My question is:
- Why aren't required files looked up in subdirectories relative to
the current file?

Well, they dhould use $LOAD_PATH, shouldn't they? Perhaps you mean
another syntax.
- Is there an easy way to simulate that in Ruby?
No.

- Can it be easily fixed in Ruby interpreter source code?

Probably. Adding syntax is easy of you know what you're doing (I
don't), but it's controversial at this stage of Ruby's life.

Search for an RCR (www.rubygarden.org) on it; create a new one if
there is none.

Personally, although I have occasionally wanted what you want, I don't
think I've (in recent times) ever actually used the __FILE__ trick for
'require'. It's more a case of taking a look at how files/directories
are laid out.

I'll have plenty of specific advice if you detail what you're trying
to do :)

Cheers,
Gavin
 
A

Andrew Walrond

The full solution is worse. To handle the case of the script being executed
via symlinks or relative paths, you need to do something like this:

require 'ftools'
dir = File.expand_path(__FILE__)
while File.symlink?(dir); dir = File.readlink(dir); end
SCRIPT_DIR = File.dirname(dir)

require SCRIPT_DIR+'/my_script.rb'
...
 
G

Gavin Sinclair

The full solution is worse. To handle the case of the script being executed
via symlinks or relative paths, you need to do something like this:
require 'ftools'
dir = File.expand_path(__FILE__)
while File.symlink?(dir); dir = File.readlink(dir); end
SCRIPT_DIR = File.dirname(dir)
require SCRIPT_DIR+'/my_script.rb'
...


require 'pathname'
dir = Pathname.new(File.expand_path(__FILE__)).realpath

Gavin
 
Z

Zoran Lazarevic

Gavin Sinclair said:
require 'pathname'
dir = Pathname.new(File.expand_path(__FILE__)).realpath

We probably need function 'require_local' because this is often used.
Or maybe an extra argument to require ( require
'myfile',:relative=>true )

require 'require_local'
require_local 'path_relative to this file'

....

require 'pathname'
def require_local filename
dir = Pathname.new(File.expand_path(__FILE__)).realpath
require File.join(dir,filename)
end


--Laza
 
A

Andrew Walrond

Even better,

dir = Pathname.new(__FILE__).expand_path.realpath

Actually, this appears to be broken when I try to use it. Example:

root@bob root # ls -l /sbin/rubyx
lrwxrwxrwx 1 root root 23 Nov 24 20:41 /sbin/rubyx -> /pkg/rubyx.1/sbin/rubyx
root@bob root # irb
irb(main):001:0> require 'pathname'
=> true
irb(main):002:0> Pathname.new('/sbin/rubyx')
=> #<Pathname:/sbin/rubyx>
irb(main):003:0> Pathname.new('/sbin/rubyx').realpath
Errno::ENOENT: No such file or directory - /sbin/pkg
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:144:in `lstat'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:144:in `realpath_rec'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:125:in `scan'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:125:in `realpath_rec'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:149:in `realpath_rec'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:125:in `scan'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:125:in `realpath_rec'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:113:in `realpath'
from (irb):3
from :0
irb(main):004:0>
 
T

Tanaka Akira

Andrew Walrond said:
Actually, this appears to be broken when I try to use it. Example:

root@bob root # ls -l /sbin/rubyx
lrwxrwxrwx 1 root root 23 Nov 24 20:41 /sbin/rubyx -> /pkg/rubyx.1/sbin/rubyx
root@bob root # irb
irb(main):001:0> require 'pathname'
=> true
irb(main):002:0> Pathname.new('/sbin/rubyx')
=> #<Pathname:/sbin/rubyx>
irb(main):003:0> Pathname.new('/sbin/rubyx').realpath
Errno::ENOENT: No such file or directory - /sbin/pkg
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:144:in `lstat'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:144:in `realpath_rec'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:125:in `scan'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:125:in `realpath_rec'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:149:in `realpath_rec'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:125:in `scan'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:125:in `realpath_rec'
from /pkg/ruby.1/lib/ruby/1.8/pathname.rb:113:in `realpath'
from (irb):3
from :0
irb(main):004:0>

It is already fixed in latest CVS version.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top