Skip to content

Instantly share code, notes, and snippets.

@judofyr
Created July 19, 2017 09:53
Show Gist options
  • Save judofyr/0c9f4067f00622d6bd942142191d2b87 to your computer and use it in GitHub Desktop.
Save judofyr/0c9f4067f00622d6bd942142191d2b87 to your computer and use it in GitHub Desktop.
require_relative 'import'
B = import "b"
p defined?(::User)
user = B.new_user
p user
class User
end
def new_user
User.new
end
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