ruby newby

U

Uwe Schmitt

Hi,

I'm trying to write a parser for cooking recipes.
It looks like this:

-----
require 'bspmess'
class Parser

STATES = [ :FIND_START, :SCAN_HEADER]

methods = { :FIND_START => self.find_start,
:SCAN_HEADER => self.scan_header
}

def initialize(msg)
@msg = msg
@state = :FIND_START
end

def run
@msg.each { |line|
scan line
}
end

def scan(line)
methods[@state].call(line)
end

def find_start(line)
puts "find_start "
@state = :SCAN_HEADER
end

def scan_header(line)
puts "scan header"
end


end


p = Parser.new(MSG)
p.run

-----

I get an error when the scan method is called first:
undefined method `find_start' for Parser:Class (NoMethodError)

Who can help me ?

Thanks in advance,

Uwe
 
E

Edward Faulkner

--5vNYLRcllDrimb99
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

methods = { :FIND_START => self.find_start,
:SCAN_HEADER => self.scan_header
}

This doesn't do what you want. It's trying to call the "find_start"
and "scan_header" methods on the Parser Class object. There are
several possibilities to do this correctly:

1. Call self.instance_method:)find_start) to get an unbound method,
which you'll need to #bind before calling it.

2. Construct your "methods" table in #initialize, and use
self.method:)find_start) to get a bound method.

3. Don't build a method table at all. Do this instead:

def scan(line)
self.send(@state,line)
end

regards,
Ed

--5vNYLRcllDrimb99
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDmGZtnhUz11p9MSARAqDcAKCyT2XHR1ZKaI4mmuSEHqX2M8x5JgCgyAkq
H5yykrQpu0ECy9hPAFgfEMY=
=gsdK
-----END PGP SIGNATURE-----

--5vNYLRcllDrimb99--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top