Consider the following code:
a.rb: puts "a"
b.rb: require 'a' puts "b"
test.rb: require './a' require './b'
This will print:
a a bbecause a.rb is loaded first by the name 'a.rb' and then by the name './a.rb'. This is probably not what is intended, and the only workarounds are:
The above code is probably not a good idea to begin with (requring files from the current directory is bad, in case the user runs from a different directory than expected), but it is a simple way to demonstrate the problem.
def find_file_in_path(file, path=$:, extensions=['', '.so', '.rb'])
path.each do |dir|
pathname = File.join(dir, file)
extensions.each do |ext|
filename = pathname + ext
if File.exists?(filename) then
return File.expand_path(filename)
end
end
end
return file
end
def realpath(file)
if not File.respond_to?(:readlink) then
return file
end
total = ''
File.expand_path(file).split(File::SEPARATOR).each do |piece|
next if piece == ""
if File.symlink?(total + File::SEPARATOR + piece) then
total += File::SEPARATOR + piece
begin
total = File.expand_path(
File.readlink(total),
File.dirname(total))
end while File.symlink?(total)
else
total << File::SEPARATOR + piece
end
end
return total
end
$__old_require__ = method(:require)
def require(file)
file_to_require = find_file_in_path(file)
$__old_require__.call(realpath(file_to_require))
end
Back to RCRchive.
RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog