How to define a function-generating function?

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!

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. [email protected]:

Gaspard B. wrote:

printf(msg,name)

end
end

Yes, exactly what I want! Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs