-
-
Save mattyoho/305543 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
# not sure if i like this or not | |
# but it was fun to write! | |
# :Foo => Foo, :Foo, :Bar => Foo::Bar, etc | |
def with_foo_like_class(*args) | |
klass = if args.size == 1 | |
klass_obj = Class.new | |
Object.const_set(args[0], klass_obj) | |
else | |
klass_sym = args.pop | |
parents = args.inject([]) do |namespaces, name| | |
(namespaces.last || Object).module_eval do | |
namespaces << const_set(name, Module.new) | |
end | |
end | |
parents.last.module_eval do | |
klass_obj = Class.new | |
const_set(klass_sym, klass_obj) | |
end | |
end | |
klass.class_eval { extend Foo } | |
yield(klass) | |
Object.send(:remove_const, args[0]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment