Forum: Ruby on Rails function names contain regex?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Pete F. (Guest)
on 2006-03-19 08:29
(Received via mailing list)
Is it possible to support something like the following:

def validate_address{#/d/#)
    x = {#1}
    miscfunction(x)
end

And yes, I totally just made up some bogus syntax - I have no idea if a
variable function name containing a regular expression is possible...
perhaps through some trick of aliasing?

I figure it must be possible, due to the model.find_x_and_y() methods in
ActiveRecord.

Bonus question: Is there a way for the code inside a function to
retrieve
the name of the function? And if so, if it was called through an alias,
would it return the name of the alias or the name of the original
function?

Pete
Jamis B. (Guest)
on 2006-03-19 15:54
(Received via mailing list)
Pete,

There's no syntax for it, but you can implement it yourself using
method_missing (which is what ActiveRecord does):

   class Foo
     def validate_address(parameter, a)
       puts "#{parameter}: #{a}"
     end

     def method_missing(sym, *args, &block)
       if sym.to_s =~ /^validate_address_(.*)$/
         validate_address($1, *args, &block)
       else
         super
       end
     end
   end

   f = Foo.new
   f.validate_address_hello("world")
   #-> "hello: world"

- Jamis
This topic is locked and can not be replied to.