Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail From: 7stud -- Newsgroups: comp.lang.ruby Subject: Re: Making a simple parser Date: Mon, 18 Apr 2011 13:24:32 -0500 Organization: Service de news de lacave.net Lines: 63 Message-ID: References: <87adbac7061a00a72e282c160ed42414@ruby-forum.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1303151086 94489 65.111.164.187 (18 Apr 2011 18:24:46 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Mon, 18 Apr 2011 18:24:46 +0000 (UTC) In-Reply-To: <87adbac7061a00a72e282c160ed42414@ruby-forum.com> X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 381791 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:3113 I'm pretty unclear about what you are trying to do, but maybe this will help: class InputFormat @children = [] def self.children @children end def initialize(input) @input = input end def parse InputFormat.children.each { |child| child.parse(@input) if child.supported? } end def self.inherited(sub_class) @children << sub_class.new('dummy') end end class InputFormatA < InputFormat def supported? true end def parse(str) puts "InputFormatA is parsing #{str}" end end class InputFormatB < InputFormat def supported? true end def parse(str) puts "InputFormatB is parsing #{str}" end end input = InputFormat.new('hello world') input.parse --output:-- InputFormatA is parsing hello world InputFormatB is parsing hello world Note that when inherited() is called, the methods of the subclass are not defined yet, so the inherited initialize() is called. -- Posted via http://www.ruby-forum.com/.