# Module\_function

**URL:** <https://www.ruby-forum.com/t/module-function/135415>\
**Category:** IronRuby\
**Created:** [April 22, 2008, 12:58am UTC](https://www.ruby-forum.com/t/module-function/135415 "2008-04-22T00:58:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [April 22, 2008, 12:58am UTC](https://www.ruby-forum.com/t/module-function/135415/1 "2008-04-22T00:58:56Z")

</div>

Is anyone currently working on or have any ideas regarding the  
implementation of Module::module\_function?  
(this is where I’m currently stuck in trying to execute setup.rb for  
gems)

module\_function is meant to take a list of names of instance methods and  
make then also callable as class (or singleton) methods.

It’s easy enough to locate the existing methods using something like:

RubyMethodInfo method =  
(RubyMethodInfo)module.ResolveMethod(method\_name);

(BTW, why doesn’t ResolveMethod return a RubyMethodInfo?)

However, I’m not entirely sure how to programmatically create a new  
class method  
(in [Ruby.NET](http://Ruby.NET) we had a helper method that would create a singleton for a  
module if one didn’t already exist, but I’m not sure what the equivalent  
of this is in IronRuby)

Anyway, it seems that invoking:

module.SingletonClass.DefineMethod( …)

creates a new method in the right place (provided SingletonClass has  
been properly initialized).

However, when I try calling such a method as a class method I get a  
wrong number of arguments exception, presumably because different  
parameters are expected depending on whether it is called as an instance  
method or as a class method.

Any ideas?

Cheers, Wayne.

---

<div class="post-metadata">

**Author:** ![Wayne\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/b5ac83/32.png) [@Wayne\_K](https://www.ruby-forum.com/u/Wayne_K)\
**Post date:** [April 22, 2008, 9:39am UTC](https://www.ruby-forum.com/t/module-function/135415/2 "2008-04-22T09:39:05Z")

</div>

I know that a IronRuby expects singleton methods to have a signature of  
the  
form  
SomeReturnType MySingletonMethod(CodeContext context, RubyClass  
klass, Other parameters, …)  
And if you don’t create them like this, e.g. forget the context or klass  
parameters you do get this problem.  
And instance methods, canonically to have the form  
SomeReturnType MyInstanceMethod(CodeContext context, object self,  
Other parameters, …)  
Although instance methods seem to be more resilient to alternatives,  
such as  
not including the context parameter.

I suspect that you will need to create a wrapper method around the  
method  
that you are converting in module\_function so that it has the correct  
signature.  
Pete
