# Creating method dynamically

**URL:** <https://www.ruby-forum.com/t/creating-method-dynamically/147711>\
**Category:** Ruby\
**Created:** [September 4, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/creating-method-dynamically/147711 "2008-09-04T15:20:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [September 4, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/creating-method-dynamically/147711/1 "2008-09-04T15:20:17Z")

</div>

Hello i have the following ruby code, to create class method dynamically  
for my module M:

module M  
def self.create\_method(name, &block)  
eigen=class \<\< self; self; end

```
eigen.module_eval do
  define_method(name, &block)
end

```

end  
end

Now i am writing a C extension and need exactly this functionality for  
my C version of create\_method() how do i do it in C? rb\_define\_method()  
expects a pointer to a C function, but i want to define a new method  
that takes a rubyblock instead, so i think rb\_define\_method() is not the  
right route.

Any help would be very much appreciated,

john

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [September 4, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/creating-method-dynamically/147711/2 "2008-09-04T20:23:25Z")

</div>

On Sep 4, 2008, at 06:14 , John M. wrote:

> end  
> end  
> end
> 
> Now i am writing a C extension and need exactly this functionality for  
> my C version of create\_method() how do i do it in C?

go read the C code for define\_method.
