From: Andreas Lundgren Date: 2011-05-18T22:46:02+09:00 Subject: Generating Functions in Ruby Hi! I'm looking for a construction in Ruby to generate a (big) set of functions. I need these functions to be class methods and called by unique names since they are called via an external API. As an example, let me create two functions that hard codes an addition and then uses this in a small test program. def FivePlusFour return 5+4 end def OnePlusSeven return 1+7 end puts("Five + Four is " + FivePlusFour().to_s) puts("One + Seven is " + OnePlusSeven().to_s) I would like a way to generate the methods in runtime. As an old C- programmer, I'm looking for something like this: # Define a macro define SUM_TEMPLATE(namn,x,y) \\ def namn \\ return x+y \\ end # Create each function with a one-liner in runtime. SUM_TEMPLATE(FivePlusFour, 5, 4) SUM_TEMPLATE(OnePlusSeven, 1, 7) # Test program puts("Five + Four is " + FivePlusFour().to_s) puts("One + Seven is " + OnePlusSeven().to_s) Is this possible in Ruby? (Please don't reply how to solve this silly example in a better way, the true use case is of cause much more complex. But conceptually it's the same.) Best Regards, Andreas Lundgren - Sweden