Skip to content

Instantly share code, notes, and snippets.

@mattyoho
Forked from ryanbriones/gist:305314
Created February 16, 2010 14:11
Show Gist options
  • Save mattyoho/305543 to your computer and use it in GitHub Desktop.
Save mattyoho/305543 to your computer and use it in GitHub Desktop.
# 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