Dynamic method creation

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 _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

On 6/21/07, Matt [email protected] wrote:


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

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 C. wrote:

On 6/21/07, Matt [email protected] wrote:


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

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!