[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

load/require into module/class, revisted

Trans

12/18/2006 6:31:00 AM

revisting a topic brought up a few times before. a good reference
being:


http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/74f323436b2bc85f/0c7780b698866cc5?lnk=gst&q=load+import&rnum=4&hl=en#0c7780...

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