# How to call instance method from within Treetop gem

**URL:** <https://www.ruby-forum.com/t/how-to-call-instance-method-from-within-treetop-gem/163825>\
**Category:** Ruby\
**Created:** [March 25, 2009, 9:57am UTC](https://www.ruby-forum.com/t/how-to-call-instance-method-from-within-treetop-gem/163825 "2009-03-25T09:57:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 25, 2009, 9:57am UTC](https://www.ruby-forum.com/t/how-to-call-instance-method-from-within-treetop-gem/163825/1 "2009-03-25T09:57:13Z")

</div>

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

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 25, 2009, 11:01am UTC](https://www.ruby-forum.com/t/how-to-call-instance-method-from-within-treetop-gem/163825/2 "2009-03-25T11:01:44Z")

</div>

Tom C. wrote:

> SyntaxNode+HelpMain0 offset=0, “.h”:Treetop::Runtime::SyntaxNode  
> 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

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [March 25, 2009, 11:56am UTC](https://www.ruby-forum.com/t/how-to-call-instance-method-from-within-treetop-gem/163825/3 "2009-03-25T11:56:18Z")

</div>

Justin C. wrote:

> > @parser = Base\_grammarParser.new # \< = Treetop class instance  
> > (eval):71:in `act': undefined method`showhelp’ for
> 
> 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.  
> That’s the problem I didn’t know how to address!  
> def showhelp  
> }  
> @parser = Base\_grammarParser.new  
> .\* {

Justin,

I am in your debt. I didn’t have much faith that I could explain my  
problem well enough, and simply enough, that I’d get a response. Your  
solution made sense to me (I’d wanted to do what your code does, but  
didn’t know how). With a minor adjustment, it worked, which was a bit  
stunning. Here’s the final version (a prototype only, but that’ll get me  
launched):

#this - in my main class method  
Treetop.load ‘…/data/sn\_grammar’  
@parser = Base\_grammarParser.new

#this in my secondary method  
if parse\_tree = @parser.parse( @opt )  
parse\_tree.act(self)  
else…

#this in my grammar rule

# display main help

rule help\_main  
‘.h’ {  
def act( sn )  
#puts ‘help\_main’  
sn.showhelp( [‘0’, ‘0c’, ‘0a’, ‘0q’, ‘0v’, ‘0f’, ‘0m’, ‘0u’] )  
end  
}  
end

#and this in my main, as suggested by Markus on the Treetop list -  
class Treetop::Runtime::SyntaxNode  
#include SN  
def act  
elements.each { |e|  
e.act(self) if e.respond\_to? :act }  
end  
end

Now I can write my grammar and get this thing working for me.

I much apppreciate your superior knowledge, and your generosity in  
sharing it.

t.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```
