From: "trans. (T. Onoma)" Date: 2004-11-25T16:17:36+09:00 Subject: Re: Generalizing and Organizing Code On Tuesday 23 November 2004 10:33 am, Robert Klemme wrote: | I'd keep the option to put state in there, because you might need it some | time. Good point. | > yet there is only ever one of | > each, so they aren't classes. | | But you can make them classes nevertheless. True. | If I'd strive for the most OO solution I'd do this: | | class Parser | class BaseProc | def self.children() @children ||= [] end | def self.inherited(cl) self.children << cl end | | attr_accessor :parent | [snip] | | BaseProc.children.each do |cl| | define_method(cl.name.downcase.gsub(/^.*::/, '')) { @handlers[cl] } | end | | | attr_accessor :document_stack | attr_reader :section, :paragraph | | def initialize | @handlers = Hash.new {|h,k| h[k]=k.new(self)} | end | | end Took me a few minutes to get this one. I've never used Hash block form of instantiation before. The use of parent is also quite insightful. So I ended up doing it in part as you suggest, with a bit of David's solution mixed in too, and my own of course. I'm still not perfectly satisfied but it's definitely improving. Thanks and Happy Givings as well, T.