# How to call a module method dynamically

**URL:** <https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975>\
**Category:** Ruby\
**Created:** [February 21, 2009, 5:59pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975 "2009-02-21T17:59:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 5:59pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/1 "2009-02-21T17:59:18Z")

</div>

I know about object instantiation and the send method, but the methods I  
want to call are not in a class. They live in a module.

module Foo

module Bar

```
def mymethod
  "xyz"
end

```

end

end

I can call it statically, like:

myvar = Foo::Bar::xyz

But how can I call it dynamically. Basically I have some batch jobs  
kicked off by a rake task. I want to use a single rake task to kick off  
any job, so I don’t have to have a task for every single job, I can just  
pass the job name (i.e. the name of the module method) to my one task  
and have it kicked off.

Any help is appreciated! I’m missing something simple, but I can’t find  
it on google or in pickaxe.

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 7:22pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/2 "2009-02-21T19:22:27Z")

</div>

> I can just pass the job name (i.e. the name of the module method)

Couldn’t you also pass a block?

do\_something {Foo::Bar.mymethod}

You can get the method object via instance\_method but then you have an  
unbound method. Or you could turn that method into a module\_function  
which you could invoke via MyModule.send(:mymethod). But IMHO that’s  
weird.

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 7:28pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/3 "2009-02-21T19:28:55Z")

</div>

Tom L. wrote:

> > I can just pass the job name (i.e. the name of the module method)
> 
> Couldn’t you also pass a block?
> 
> do\_something {Foo::Bar.mymethod}
> 
> You can get the method object via instance\_method but then you have an  
> unbound method. Or you could turn that method into a module\_function  
> which you could invoke via MyModule.send(:mymethod). But IMHO that’s  
> weird.

Even weirder is monkey-patching Object with my module so I can just use  
“send”, which is what I did.

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 7:37pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/4 "2009-02-21T19:37:49Z")

</div>

On Sat, Feb 21, 2009 at 10:28 PM, Adrian Klingel  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> end
> 
> I can call it statically, like:
> 
> myvar = Foo::Bar::xyz

I’m going to assume you meant Foo::Bar::mymethod, but even that wouldn’t  
work.

The way you’ve defined mymethod above makes it an instance method.  
The only way to access a module instance method is to mix the module  
into a class/object. If you need an instance method to double as a  
class method, Module#module\_function can help:

$: irb  
01\> module Foo  
02\> def mymethod; “xyz”; end  
03\> end  
 → nil  
04\> Foo::mymethod  
NoMethodError: undefined method `mymethod' for Foo:Module from (irb):4 from /usr/local/ruby1.9/bin/irb:12:in `’  
05\> module Foo  
06\> module\_function :mymethod  
07\> end  
 → Foo  
08\> Foo::mymethod  
 → “xyz”

Having said all that, you haven’t indicated any reason for these be  
instance methods. If they aren’t intended to be mixed into anything  
and the Module is essentially just a namespace in which to define your  
methods, they might as well be class methods.

If so, then Object#send will work just fine:

09\> Foo.send(:mymethod)  
 → “xyz”

> I want to use a single rake task to kick off  
> any job, so I don’t have to have a task for every single job

Reconsider. One of the nice things about rake tasks is they double as  
documentation. Being able to run ‘rake -T’ and see a listing of  
available commands is really useful, and more durable than having to  
remember (or document elsewhere) the names of the available methods.  
And since rake tasks are created dynamically, some simple reflection  
and loop will ensure the tasks always match the methods available:

namespace :command  
Foo.methods(false).each do |method\_name|  
desc “run #{method\_name}”  
task method\_name do  
Foo.send(:method\_name)  
end  
end  
end

I’m still not sure i understand your problem (!), but i hope some part  
of all the above helps 🙂

lasitha.

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 7:43pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/5 "2009-02-21T19:43:42Z")

</div>

On Sat, Feb 21, 2009 at 4:58 PM, Adrian Klingel  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> end
> 
> I can call it statically, like:
> 
> myvar = Foo::Bar::xyz

Can you? I get:

irb → myvar = Foo::Bar::xyz  
NoMethodError: undefined method `xyz’ for Foo::Bar:Module

Perhaps you meant:

module Foo  
module Bar  
def self.mymethod  
“xyz”  
end  
end  
end

myvar = Foo::Bar::mymethod # =\> “xyz”

> But how can I call it dynamically.

myvar = Foo::Bar.send(“mymethod”) # =\> “xyz”

Regards,  
Sean

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/6 "2009-02-21T19:49:33Z")

</div>

Ahhh, I’m missing the “self”, thanks Sean. But here’s the other issue.  
At runtime I don’t know what module is going to be used. It could be  
one of several. I don’t know that it’s Foo::Bar. It might be Foo:Bat or  
Bat::Fat.

Is the answer then to do a dynamic include on Foo::Bar and just call  
plain old “send”?

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 8:02pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/7 "2009-02-21T20:02:39Z")

</div>

On Sun, Feb 22, 2009 at 12:19 AM, Adrian Klingel  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ahhh, I’m missing the “self”, thanks Sean. But here’s the other issue.  
> At runtime I don’t know what module is going to be used. It could be  
> one of several. I don’t know that it’s Foo::Bar. It might be Foo:Bat or  
> Bat::Fat.

You can use Module#const\_get to look up a module by name.

It helps if you can arrange for all the possible modules to be  
contained in a single namespace - it can be Foo::Bar or Foo:Baz, but  
not X::Y. That way you can look up the module with  
Foo.const\_get(name).

If it can be any arbitrary module (bad idea), you’ll have to call  
const\_get on Kernel.

Cheers,  
lasitha.

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 9:05pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/8 "2009-02-21T21:05:53Z")

</div>

Yeah, that’s definitely new to me. Thanks stud.

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/9 "2009-02-21T20:49:14Z")

</div>

Adrian Klingel wrote:

> Ahhh, I’m missing the “self”

Ahhh, I guess you don’t need this then:

module Foo

module Bar  
def mymethod  
“xyz”  
end  
end

end

str = “Foo::Bar::mymethod”

pieces = str.split(“::”)  
method\_name = pieces.pop  
module\_name = pieces.join(“::”)

mod = eval(module\_name)

mod.module\_eval do  
module\_function method\_name.to\_sym  
end

puts mod.module\_eval(method\_name)

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/10 "2009-02-21T21:28:51Z")

</div>

Adrian Klingel wrote:

> Yeah, that’s definitely new to me. Thanks stud.

Me too. Can anyone explain why I can do this:

num = 1  
puts Object.module\_eval(“num”) #=\>1

def f  
“xyz”  
end

puts Object.module\_eval(“f”) #=\>xyz

but I can’t do this:

module Foo  
def mymethod  
“xyz”  
end  
end

puts Foo.module\_eval(“mymethod”)

–output:–  
`module_eval': undefined local variable or method `mymethod’ for  
Foo:Module (NameError)

That result and the result from my previous example seem to imply that  
free standing def’s are added to Object’s singleton class, i.e. they are  
class methods. But in pickaxe2, p.346 it says:

* * *

## Outside a class or module definition, a definition with an unadorned method name is added as a private method to class Object, and hence may be called in any context without an explicit receiver.

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 21, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/11 "2009-02-21T21:33:29Z")

</div>

What about:

puts Object.module\_eval(“Foo:method”)

---

<div class="post-metadata">

**Author:** ![huckyducky](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b19c9b/32.png) [@huckyducky](https://www.ruby-forum.com/u/huckyducky)\
**Post date:** [February 22, 2009, 7:15am UTC](https://www.ruby-forum.com/t/how-to-call-a-module-method-dynamically/160975/12 "2009-02-22T07:15:28Z")

</div>

On Sun, Feb 22, 2009 at 1:58 AM, 7stud – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> –output:–  
> `module_eval': undefined local variable or method `mymethod’ for  
> Foo:Module (NameError)
> 
> That result and the result from my previous example seem to imply that  
> free standing def’s are added to Object’s singleton class, i.e. they are  
> class methods.

No, they are private instance methods of Object:

$: irb  
01\> def free\_standing; end  
 → nil  
02\> Object.private\_instance\_methods.grep /free\_standing/  
 → [:free\_standing]

I suppose the confusion comes about because of the line:

> puts Object.module\_eval(“f”) #=\>xyz

The reason this works is not that f was defined as a class method.  
Its because the context in which f is resolved inherits from Object.

03\> Object.module\_eval do  
04\> puts “self: #{self}”  
05\> puts “ancestors:”, self.class.ancestors  
06\> end  
self: Object  
ancestors:  
Class  
Module  
Object  
PP::ObjectMixin  
Kernel  
BasicObject

The current object inherits from Object (just like all objects) and  
the method f was defined as an instance method of Object, so it is  
available in this context.

It is of course confusing that the current object is Object and also  
inherits from Object. How ruby keeps all that straight is beyond my  
knowledge :), but it does make sense intuitively.

Cheers,  
lasitha
