[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to define a function-generating function?

Josip Gracin

9/14/2007 9:01:00 AM

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!
2 Answers

Gaspard Bucher

9/14/2007 9:34:00 AM

0

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 Gracin <gracin@tel.fer.hr>:
> 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!
>
>

Josip Gracin

9/14/2007 9:57:00 AM

0

Gaspard Bucher wrote:
> 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

Yes, exactly what I want! Thanks!