Created
July 21, 2013 15:41
-
-
Save jeremywrowe/6048909 to your computer and use it in GitHub Desktop.
This is totally insecure and just a proof of concept. I find myself annoyed that parent modules have to exist prior to creating their children. Modules are a form of namespacing code and we should be able to organize that code however we want.
This file contains hidden or 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
#!/usr/bin/env ruby | |
def create_module(representation, &block) | |
parts = representation.split("::") | |
(0...parts.size).each do |idx| | |
const = parts[(0..idx)].join("::") | |
eval "::#{const} = Module.new unless defined?(::#{const})" | |
end | |
if block_given? | |
Kernel.const_get(representation).instance_eval(&block) | |
end | |
end | |
create_module("Prehistoric::Cave::People") do | |
class << self | |
def fred | |
p("FLLLLLIIINSTONE") | |
end | |
alias :wilma :fred | |
end | |
end | |
create_module("Wow::This::Is::Really::Deep") do | |
class << self | |
def yep | |
p("woot") | |
end | |
end | |
end | |
Prehistoric::Cave::People.fred | |
Prehistoric::Cave::People.wilma | |
Wow::This::Is::Really::Deep.yep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment