revisting a topic brought up a few times before. a good reference
being:
after considerable thought i think the interface that makes the most
sense is #module_load / #module_require. below is an implementation
please suggest improvements to the code.
thanks,
t.
module Module
Load file into module/class namespace.
def module_load( path )
if path =~ /^[/~.]/
file = File.expand_path(path)
else
$LOAD_PATH.each do |lp|
file = File.join(lp,path)
break if File.exist?(file)
file = nil
end
end
module_eval(File.read(file))
end
Require file into module/class namespace.
def module_require( path )
if path =~ /^[/~.]/
file = File.expand_path(path)
else
$LOAD_PATH.each do |lp|
file = File.join(lp,path)
break if File.exist?(file)
file += ‘.rb’
break if File.exist?(file)
file = nil
end
end
@loaded ||= {}
if @loaded.key?(file)
false
else
@loaded[file] = true
module_eval(File.read(file))
true
end
end
end
class Class
alias class_load module_load
alias class_require module_require
end