Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!news.cgarbs.de!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail From: 7stud -- Newsgroups: comp.lang.ruby Subject: Re: Generating Functions in Ruby Date: Wed, 18 May 2011 14:44:45 -0500 Organization: Service de news de lacave.net Lines: 46 Message-ID: <57836d5d927311efda42e4036904bf8d@ruby-forum.com> References: <5e21af14-befb-406e-b1c3-6758fe1527ab@s14g2000vbi.googlegroups.com> NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1305747923 94500 65.111.164.187 (18 May 2011 19:45:23 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Wed, 18 May 2011 19:45:23 +0000 (UTC) In-Reply-To: X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 383449 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <57836d5d927311efda42e4036904bf8d@ruby-forum.com> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:4742 Robert K. wrote in post #999483: > On Wed, May 18, 2011 at 3:46 PM, Andreas Lundgren > wrote: >> return 5+4 >> I would like a way to generate the methods in runtime. As an old C- >> 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? > > def SUM_TEMPLATE(name, x, y) > class < end > >> I need these functions to be class methods ..of what class? Perhaps something like this: def SUM_TEMPLATE(the_class, meth_name, x, y) class_obj = Object.const_get(the_class) class_obj.singleton_class.class_eval do define_method(meth_name) do x + y end end end class Dog end SUM_TEMPLATE('Dog', 'FivePlusFour', 5, 4) puts Dog.FivePlusFour --output:-- 9 -- Posted via http://www.ruby-forum.com/.