Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3118

Re: Making a simple parser

From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: Making a simple parser
Date Mon, 18 Apr 2011 15:22:05 -0500
Organization Service de news de lacave.net
Lines 77
Message-ID <27b5b9eb5e5b767bc8b900636c29cbc2@ruby-forum.com> (permalink)
References <87adbac7061a00a72e282c160ed42414@ruby-forum.com> <da69f5afa6794cb197ccceb8072e2fcc@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 1303158145 7267 65.111.164.187 (18 Apr 2011 20:22:25 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Mon, 18 Apr 2011 20:22:25 +0000 (UTC)
In-Reply-To <da69f5afa6794cb197ccceb8072e2fcc@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 381796
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <27b5b9eb5e5b767bc8b900636c29cbc2@ruby-forum.com>
Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!talisker.lacave.net!lacave.net!not-for-mail
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:3118

Show key headers only | View raw


7stud -- wrote in post #993588:
>
> Note that when inherited() is called, the methods of the subclass are
> not defined yet, so if you create objects of the subclass inside
> inherited(), the initialize() method in the parent is called.
>

And you can get around that problem by letting InputFormat#parse create 
the objects:

class InputFormat
  @children = []

  def self.children
    @children
  end

  def initialize(input)
    @input = input
  end

  def parse
    InputFormat.children.each { |child|
      instance = child.new
      instance.parse(@input) if instance.supported?
    }
  end

  def self.inherited(sub_class)
    @children << sub_class
  end
end



class InputFormatA < InputFormat
  def initialize
    puts "Initializing instance of #{self.class}"
  end

  def supported?
    true
  end

  def parse(str)
    puts "InputFormatA is parsing #{str}"
  end
end

class InputFormatB < InputFormat
  def initialize
    puts "Initializing instance of #{self.class}"
  end

  def supported?
    true
  end

  def parse(str)
    puts "InputFormatB is parsing #{str}"
  end
end



input = InputFormat.new('hello world')
input.parse

--output:--
Initializing instance of InputFormatA
InputFormatA is parsing hello world
Initializing instance of InputFormatB
InputFormatB is parsing hello world

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Making a simple parser Felipe Balbi <balbif@gmail.com> - 2011-04-16 14:52 -0500
  Re: Making a simple parser 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-16 22:31 -0500
  Re: Making a simple parser jake kaiden <jakekaiden@yahoo.com> - 2011-04-17 12:01 -0500
    Re: Making a simple parser Felipe Balbi <balbif@gmail.com> - 2011-04-17 15:22 -0500
      Re: Making a simple parser jake kaiden <jakekaiden@yahoo.com> - 2011-04-17 21:10 -0500
  Re: Making a simple parser Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-18 02:22 -0500
    Re: Making a simple parser Felipe Balbi <balbif@gmail.com> - 2011-04-19 16:03 -0500
  Re: Making a simple parser 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-18 13:24 -0500
    Re: Making a simple parser 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-18 15:22 -0500

csiph-web