# How to define a function-generating function?

**URL:** <https://www.ruby-forum.com/t/how-to-define-a-function-generating-function/114690>\
**Category:** Ruby\
**Created:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/how-to-define-a-function-generating-function/114690 "2007-09-17T13:24:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josip\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9e8a1a/32.png) [@Josip\_G](https://www.ruby-forum.com/u/Josip_G)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/how-to-define-a-function-generating-function/114690/1 "2007-09-17T13:24:13Z")

</div>

Hello! A newbie question.

Here’s what I’d like to be able to do.

#============================================  
def\_init\_function( :system\_init ) do |x,y|  
…something…  
end

def\_init\_function( :timers\_init ) do |a|  
…another body…  
end

…and later…  
system\_init(2,3)  
timers\_init(0)  
#============================================

In addition to defining a function, the def\_init\_function() will perform  
some additional book-keeping. The question is: how do implement  
def\_init\_function()? (The syntax need not be exactly like in the  
example above.)

I’ve tried various ways (using define\_method, eval) but I couldn’t make  
it do what I want.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Josip\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9e8a1a/32.png) [@Josip\_G](https://www.ruby-forum.com/u/Josip_G)\
**Post date:** [September 17, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/how-to-define-a-function-generating-function/114690/2 "2007-09-17T13:24:57Z")

</div>

Is this what you are looking for ?  
class Module  
def funky\_define\_method(\*args, &block)  
puts “#{args[0]} is being defined”  
define\_method(\*args, &block)  
end  
end

class Parrot  
funky\_define\_method(:hello) do |msg, name|  
printf(msg,name)  
end  
end

Parrot.new.hello(“Hello %s !\n”, ‘Josip’)

2007/9/14, Josip G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Josip\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9e8a1a/32.png) [@Josip\_G](https://www.ruby-forum.com/u/Josip_G)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/how-to-define-a-function-generating-function/114690/3 "2007-09-17T13:25:39Z")

</div>

Gaspard B. wrote:

> ```
> printf(msg,name)
> 
> ```
> 
> end  
> end

Yes, exactly what I want! Thanks!
