Dynamic method creation


#1

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


#2

On 6/21/07, Matt removed_email_address@domain.invalid 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


#3

Chris C. wrote:

On 6/21/07, Matt removed_email_address@domain.invalid 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!