How to define a function-generating function?

J

Josip Gracin

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!
 
G

Gaspard Bucher

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')
 
J

Josip Gracin

Gaspard said:
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,007
Latest member
Elvis60357

Latest Threads

Top