From: Dave Thomas Date: 2002-07-13T04:14:56+09:00 Subject: Re: Dynamic Methods "John" writes: > I wonder if anyone has any suggestions on how to create a method that > matches its name based on a regular expression. For instance: How about something like this: class Dave # This part implements the 'alias_regexp' thingie @@_method_matches = {} def self.alias_regexp(regexp, name) @@_method_matches[regexp] = name end def method_missing(name, *args) name = name.to_s @@_method_matches.each do |regexp, target| if regexp =~ name.to_s return send(target, *args) end end super end # and this demonstrates it in action def do_it puts "do it" end def dont puts "dont" end alias_regexp /no.*/, :dont alias_regexp /nyet.*/, :dont alias_regexp /yes.*/, :do_it end d = Dave.new d.no_way d.nyet_way d.yes_please Cheers Dave