From: Pit Capitain Date: 2008-07-16T16:13:34+09:00 Subject: Re: Complicated class initialization/instantiation problem. 2008/7/16 Michael T. Richter : > Basically I'm relying on several non-DRY chunks of code, each of which > can cause me problems in the future. First, in each of the Foo* classes > I'm repeating ".each { |tag| Bar.register(tag, Foo1) }". > (...) > The second, more subtle, > problem is exemplified in Foo2. Because the code is mostly boilerplate > -- I'm changing the tags and the instantiating class only -- it's very > easy to make a cut-and-paste bug like telling the registry to > instantiate Foo1 instead of Foo2 for tags 4, 5 and 6. Hello Michael, to remove the each loop you could change Bar.register to accept multiple tags, and to avoid copy and paste errors you could just use "self" instead of the classes: class Bar @registry = {} def self.register klass, *tags tags.each do |tag| @registry[tag] ||= klass end end # other stuff end class Foo1 Bar.register self, TAG1 = 1, TAG2 = 2, TAG3 = 3 # other stuff end class Foo2 Bar.register self, TAG4 = 4, TAG5 = 5, TAG6 = 6 # other stuff end Regards, Pit