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


Groups > comp.lang.ruby > #3191

Re: Making a simple parser

X-FeedAbuse http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.16.109
Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!nospam.fr.eu.org!talisker.lacave.net!lacave.net!not-for-mail
From Felipe Balbi <balbif@gmail.com>
Newsgroups comp.lang.ruby
Subject Re: Making a simple parser
Date Tue, 19 Apr 2011 16:03:34 -0500
Organization Service de news de lacave.net
Lines 86
Message-ID <ce32238c179ca9dec47c6270e291f92c@ruby-forum.com> (permalink)
References <87adbac7061a00a72e282c160ed42414@ruby-forum.com> <BANLkTi=4eHATM9H902di1y5mjcbRS5dO-g@mail.gmail.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding Quoted-printable
X-Trace talisker.lacave.net 1303247044 79240 65.111.164.187 (19 Apr 2011 21:04:04 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Tue, 19 Apr 2011 21:04:04 +0000 (UTC)
In-Reply-To <BANLkTi=4eHATM9H902di1y5mjcbRS5dO-g@mail.gmail.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 381874
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <ce32238c179ca9dec47c6270e291f92c@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:3191

Show key headers only | View raw


Hi,

"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post 
#993452:
> On Sat, Apr 16, 2011 at 9:52 PM, Felipe Balbi <balbif@gmail.com> wrote:
>>
>>  @children.each { |child|
>> def supported?
>>
>> input = InputFormat.new(ARGV[0])
>> input.parse
>>
>> As it turns out, this isn't working because AInputFormat will only
>> inherit from InputFormat at the time I actually use it, am I right ? Any
>> tips you guys could give me to achieve what I want ? (from several
>> possible input formats generate one output format)
>
> The above doesn't work, because the @children inside the instance
> method "parse" is not the same as the @children inside the class
> method "self.inherited". You have to give access to the class instance
> variable, and then use that one from the parse method (the you will

aaa, you're right :-) Good point.

> see the next problem):
>
> class InputFormat
>   class << self
>     attr_accessor :children
>   end
>
>   def initialize(input)
>     @input = input
>   end
>
>   def parse
>     self.class.children.each {|child| child.parse(@input) if
> child.supported?(@input)}
>   end
>
>   def self.inherited(child)
>     (@children ||= []) << child
>   end
> end
>
> class AInputFormat < InputFormat
>  def supported?
>    # check if we can parse this type of file
>  end
>
>  def parse
>    # parse and generate array of hashes in known format
>  end
> end
>
> ruby-1.8.7-p334 :028 > input = InputFormat.new("test")
>  => #<InputFormat:0xb738bf50 @input="test">
> ruby-1.8.7-p334 :029 > input.parse
> NoMethodError: undefined method `supported?' for AInputFormat:Class
>   from (irb):11:in `parse'
>   from (irb):11:in `each'
>   from (irb):11:in `parse'
>   from (irb):29
>
> The next problem, as you see, is that you are defining instance
> methods in the subclasses, but are calling them on the class. Maybe
> the methods parse and supported? in the children could be class
> methods, or maybe what you store in @children could be an instance of
> the class.

I'm not instantiating AInputFormat in any part of the code... so making 
those
class methods is the way to go for me :-) Thanks for the tip :-)

--
balbi

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

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next 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