From: Justin Collins Date: 2009-03-25T18:57:58+09:00 Subject: Re: how to call instance method from within Treetop gem Tom Cloyd wrote: > I'm trying to call an instance method from within a gem (Treetop), and > nothing I've tried is working (3 hours into it - no results). I'll do > my best to simplify the problem. > > I have a class instance which sets things up with this... > > Treetop.load '../data/sn_grammar' # <= this loads a data file which > will be process as Ruby, after some preprocessing > > @parser = Base_grammarParser.new # < = Treetop class instance > > My class method with the code above then calls another method in the > same class - both in my module SN, class SetNet. > > The other method then calls a method of the @parser instance, and THAT > one gives to eval some of the text in the sn_grammar file referenced > in the first code line above. That text then calls another method - > showhelp - back in my class instance. But it cannot find it. I'm not > naming it correctly, somehow. I get this error: > > (eval):71:in `act': undefined method `showhelp' for > SyntaxNode+HelpMain0 offset=0, ".h":Treetop::Runtime::SyntaxNode > (NoMethodError) > from ../lib/setnet/SN.rb:265:in `proc_command' > from ../lib/setnet/SN.rb:182:in `user_interface' > from ../lib/setnet/SN.rb:113:in `manage' > from ../lib/setnet/SN.rb:38:in `startup' > from ./setnet:26 > > I've tried every permutation I can image, to tell eval how to find my > method. No go. > > Module SN => class SetNet => instance setnet => method showhelp. <= > That's what I'm trying to get to. > > How can I tell eval, executing off in the gem directory, where this > method is found? That's my problem, I think. I'm run out of > possibilities, so I'm failing to grasp something basic most likely. > > Any help would be appreciated. > > Tom > Hi Tom, You seem to be trying to call the showhelp method from within your grammar. Is this correct? If so, note that the code within the grammar is attached to SyntaxNodes as the grammar matches your text. So your showhelp method is expected to be defined for SyntaxNodes. Here is what I think your setup is: module SN class SetNet def other_method @parser = Base_grammarParser.new @parser.parse(text).eval end def showhelp end end end grammar Base_grammar rule help .* { def eval showhelp end } end end If this is the case, you can just make eval take an argument: module SN class SetNet def other_method @parser = Base_grammarParser.new @parser.parse(text).eval(self) end def showhelp end end end grammar Base_grammar rule help .* { def eval(sn) sn.showhelp end } end end Hope that helps some. -Justin