Skip to content

Instantly share code, notes, and snippets.

@stephenprater
Created July 12, 2011 16:45
Show Gist options
  • Save stephenprater/1078389 to your computer and use it in GitHub Desktop.
Save stephenprater/1078389 to your computer and use it in GitHub Desktop.
class Factory
class << self
def new_thing name
klass = Class.new(self) do
attr_accessor :name
define_method :initialize do
@name = name
end
end
const_name = name.camelize.intern
old_klass = remove_const const_name if const_defined? const_name
old_klass = nil
self.const_set const_name, klass
klass
end
end
end
Factory.new_thing "bob"
Factory.subclasses
#[Factory::Bob]
Factory.new_thing "bob"
Factory.subclasses
#[Factory::Bob, Factory::Bob]
# Shouldn't there still only be one Factory::Bob
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment