Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

load/require into module/class, revisted

13 views
Skip to first unread message

Trans

unread,
Dec 18, 2006, 1:30:34 AM12/18/06
to
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#0c7780b698866cc5

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


0 new messages