[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dynamic method creation

Matt

6/22/2007 3:30:00 AM

Hi! Is there a way to create a regular expression type method and pull
the pieces from that? I can find how to add methods dynamically but
what I really need is something like this.

def <name>_requires id
self.requires name, id
end


def requires name, id
...
end

so i could type something like test_requires 4 and it would transmit the
parameters test and 4 to the requires method.

thanks!

Matt

--
Posted via http://www.ruby-....

2 Answers

Chris Carter

6/22/2007 3:42:00 AM

0

On 6/21/07, Matt <wilsom8@rpi.edu> wrote:
> Hi! Is there a way to create a regular expression type method and pull
> the pieces from that? I can find how to add methods dynamically but
> what I really need is something like this.
>
> def <name>_requires id
> self.requires name, id
> end
>
>
> def requires name, id
> ...
> end
>
> so i could type something like test_requires 4 and it would transmit the
> parameters test and 4 to the requires method.
>
> thanks!
>
> Matt
>
> --
> Posted via http://www.ruby-....
>
>

The following (untested) code should work.

def method_missing sym,*args,&block
sym = sym.to_s
if sym =~ /(\w+)_requires/ && args.size == 1
requires $1, *args
end
end

--
Chris Carter
concentrationstudios.com
brynmawrcs.com

Matt

6/22/2007 4:12:00 AM

0

Chris Carter wrote:
> On 6/21/07, Matt <wilsom8@rpi.edu> wrote:
>> ...
>> Posted via http://www.ruby-....
>>
>>
>
> The following (untested) code should work.
>
> def method_missing sym,*args,&block
> sym = sym.to_s
> if sym =~ /(\w+)_requires/ && args.size == 1
> requires $1, *args
> end
> end


Thank you so much! Worked perfectly!


--
Posted via http://www.ruby-....