Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #4742
| 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 -- <bbxx789_05ss@yahoo.com> |
| 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> (permalink) |
| References | <5e21af14-befb-406e-b1c3-6758fe1527ab@s14g2000vbi.googlegroups.com> <BANLkTik9J+XjqBwd8Wo0_0v3qXheQB02Tg@mail.gmail.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 | <BANLkTik9J+XjqBwd8Wo0_0v3qXheQB02Tg@mail.gmail.com> |
| 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 |
Show key headers only | View raw
Robert K. wrote in post #999483:
> On Wed, May 18, 2011 at 3:46 PM, Andreas Lundgren
> <andreas.lundgren.x@gmail.com> 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 <<self;self;end.class_eval "def #{name}; #{x} + #{y}; end"
> 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/.
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-18 06:44 -0700
Re: Generating Functions in Ruby Steve Klabnik <steve@steveklabnik.com> - 2011-05-18 09:34 -0500
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-18 09:54 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-18 14:44 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-18 20:55 -0500
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-19 08:31 +0200
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-19 13:00 -0500
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-20 01:28 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-23 05:54 -0700
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-23 16:25 -0500
Re: Generating Functions in Ruby Thomas Preymesser <thopre@gmail.com> - 2011-05-19 04:35 -0500
Re: Generating Functions in Ruby Brian Candler <b.candler@pobox.com> - 2011-05-19 10:06 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-23 16:09 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-23 20:51 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-23 21:07 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-24 00:10 -0700
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-24 00:24 -0700
Re: Generating Functions in Ruby Brian Candler <b.candler@pobox.com> - 2011-05-24 03:12 -0500
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-24 03:39 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-24 06:46 -0700
Re: Generating Functions in Ruby Robert Klemme <shortcutter@googlemail.com> - 2011-05-24 10:20 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-24 17:27 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-24 17:53 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-25 03:59 -0700
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-25 12:26 -0500
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-25 17:12 -0500
Re: Generating Functions in Ruby Andreas Lundgren <andreas.lundgren.x@gmail.com> - 2011-05-27 04:48 -0700
Re: Generating Functions in Ruby 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-24 16:54 -0500
csiph-web