-
-
Save judofyr/0c9f4067f00622d6bd942142191d2b87 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
require_relative 'import' | |
B = import "b" | |
p defined?(::User) | |
user = B.new_user | |
p user | |
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
class User | |
end | |
def new_user | |
User.new | |
end | |
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
class ModuleLoader | |
def self.instance | |
@instance ||= new | |
end | |
def initialize | |
@modules = {} | |
end | |
def import(path) | |
@modules[path] ||= load(path) | |
end | |
def load(path) | |
mod = @modules[path] = Module.new | |
code = File.read(path) | |
mod.class_eval("module_function\n#{code}", path, 0) | |
mod | |
end | |
end | |
module Kernel | |
def import(name) | |
file = "#{name}.rb" | |
ModuleLoader.instance.import(file) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment