# Simple module method call question

**URL:** <https://www.ruby-forum.com/t/simple-module-method-call-question/165789>\
**Category:** Ruby\
**Created:** [April 17, 2009, 2:01pm UTC](https://www.ruby-forum.com/t/simple-module-method-call-question/165789 "2009-04-17T14:01:54Z")\
**Posts on this page:** 7\
**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:** [April 17, 2009, 2:01pm UTC](https://www.ruby-forum.com/t/simple-module-method-call-question/165789/1 "2009-04-17T14:01:54Z")

</div>

I’m making no progress on this, and so clearly have missed something  
basic.

I have a class which requires a file containing module Utils\_db. There  
is another module ‘required’ in the program which instantiates this  
class.

I then successfully access in the class two constants - one from each  
module.

Utils::MENU  
Utils\_db::MENU

Now the trouble starts.

Right after accessing the constants, I try to call a method in one of  
the modules -

eval(“Utils.orph\_chk”) \<= I will be replacing the string with a  
construction using a variable, in the final version

This always fails. I’ve tried every trick I can think of, but always I  
get this:

…/lib/setnet/SN.rb:579:in `proc_command': undefined method`orph\_chk’  
for Utils:Module (NoMethodError)  
from …/lib/setnet/SN.rb:229:in `eval' from ../lib/setnet/SN.rb:579:in`proc\_command’  
from …/lib/setnet/SN.rb:229:in `user_interface' from ../lib/setnet/SN.rb:146:in`manage’  
from …/lib/setnet/SN.rb:68:in `startup’  
from ./setnet:28

Can someone suggest why I’m not able to get to this method?

Here’s the relevant Module, in abbreviated form:

module Utils

MENU = [  
[“orph\_chk”, “searches nodes and links for references which cannot be  
resolved, and reports them”],  
[“tree\_count”,“starting with node ‘root’, locates descendants of each  
node, displaying count at each level”],  
[“showtree”,“starting with node ‘root’, locates and displays  
descendants of each node”],  
[“tree\_orphans”,“locates links which cannot be traced to the root  
node, and displays them”]  
]

def orph\_chk  
puts ‘orph\_chk’  
end

end

Any suggestions would be much appreciated. I cannot quite figure out  
what to try next.

Thanks,

Tom

---

<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:** [April 17, 2009, 2:07pm UTC](https://www.ruby-forum.com/t/simple-module-method-call-question/165789/2 "2009-04-17T14:07:11Z")

</div>

> ```
> from ./setnet:28
> 
> ```
> 
> [“tree\_count”,"starting with node ‘root’, locates descendants of each
> 
> end

You’ve defined an instance method, not a module method. The method as  
you  
define it will appear in classes that use ‘include Utils’. To get a  
module  
method, define it on self:

module Utils  
MENU = […]

def self.orph\_chk  
# …  
end  
end

---

<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:** [April 17, 2009, 2:17pm UTC](https://www.ruby-forum.com/t/simple-module-method-call-question/165789/3 "2009-04-17T14:17:10Z")

</div>

On Fri, Apr 17, 2009 at 1:00 PM, Tom C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> from ../lib/setnet/SN.rb:579:in `proc_command'
> 
> ```
> 
> Tom

Hi,

Why are you eval’ing this? This works:

eval(“include Utils; Utils.orph\_chk”)

I would think it has something to do with your binding when you eval.

Ben

---

<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:** [April 17, 2009, 4:41pm UTC](https://www.ruby-forum.com/t/simple-module-method-call-question/165789/4 "2009-04-17T16:41:37Z")

</div>

Ben L. wrote:

> > Utils:Module (NoMethodError)
> > 
> > Thanks,
> > 
> > Tom
> 
> Hi,
> 
> Why are you eval’ing this?  
> Because the name of one of my two modules isn’t known until class  
> instantiation time. It could literally be anything. That said, I’m not  
> at all sure my approach is the best. It’s just the only one I could  
> think of. It’s also my very first use of eval, and it well may be poorly  
> chosen.  
> This works:
> 
> eval(“include Utils; Utils.orph\_chk”)

Hmm. I tried that, and got some kind of method error on the “include”. I  
studied it for a good long while and simply couldn’t see the problem.

> I would think it has something to do with your binding when you eval.

Yeah, me two. I’ve never even thought about binding issues before, but I  
sure am now!

Thanks for your thoughts.

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)

```

---

<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:** [April 17, 2009, 4:41pm UTC](https://www.ruby-forum.com/t/simple-module-method-call-question/165789/5 "2009-04-17T16:41:02Z")

</div>

James C. wrote:

> > ```
> > from ./setnet:28
> > 
> > ```
> > 
> > [“tree\_count”,"starting with node ‘root’, locates descendants of each
> 
> MENU = […]

Well. Live and learn. Very simple solution, but I sure wasn’t looking in  
THAT corner of the universe.

Thank you very much.

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)

```

---

<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:** [April 18, 2009, 5:47pm UTC](https://www.ruby-forum.com/t/simple-module-method-call-question/165789/6 "2009-04-18T17:47:15Z")

</div>

Suraj K. wrote:

> > ```
> > # ...
> > 
> > ```
> 
> end
> 
> module\_function :orph\_chk  
> end
> 
> Now you can either call the method directly (Utils.orph\_chk) or mix it  
> in (include Utils; orph\_chk). So you can have your cake and eat it too!

Hmmm. This is most interesting. Ruby amazes me every day. So much  
built-in cleverness. And now this, which I’ve never seen anywhere. I’ll  
study up on it. Thanks very much for the heads up,

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:** [April 18, 2009, 1:18am UTC](https://www.ruby-forum.com/t/simple-module-method-call-question/165789/7 "2009-04-18T01:18:06Z")

</div>

James C. wrote:

> You’ve defined an instance method, not a module method. The method as  
> you define it will appear in classes that use ‘include Utils’. To get a  
> module method, define it on self:
> 
> module Utils  
> MENU = […]
> 
> def self.orph\_chk  
> # …  
> end  
> end

Alternatively, you could use module\_function() to make the instance  
method a class method on the module:

module Utils  
def orph\_chk  
# …  
end

module\_function :orph\_chk  
end

Now you can either call the method directly (Utils.orph\_chk) or mix it  
in (include Utils; orph\_chk). So you can have your cake and eat it too!
