Created
April 9, 2009 23:17
-
-
Save myobie/92814 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Namespace | |
# 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 | |
def require(path) | |
self.module_require(path) | |
end | |
def load(path) | |
self.module_load(path) | |
end | |
end | |
module Root | |
end | |
module C | |
extend Namespace | |
module_require 'm' | |
end | |
### Contents of m.rb | |
# | |
# class A | |
# end | |
# | |
# module M | |
# class B | |
# end | |
# end | |
# | |
# require 'c' | |
# | |
### Contents of c.rb | |
# | |
# class CCC | |
# end | |
# | |
# class ::DDD | |
# end | |
# | |
# class Root::EEE | |
# end | |
# | |
C::A # => C::A | |
C::M::B # => C::M::B | |
C::CCC # => C::CCC | |
DDD # => DDD | |
Root::EEE # => Root::EEE | |
CCC # => | |
# ~> -:97: uninitialized constant CCC (NameError) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment