Forum: Ruby on Rails Re: 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.
0329f0570f4160517ec52060f9566cdf?d=identicon&s=25 Scott Raymond (Guest)
on 2006-03-19 07:30
(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?

Perhaps what you want is method_missing, which can intercept any
message that doesn't get caught by another method. For example:

  class Dog
    def method_missing id
      raise NoMethodError unless /say_(\w+)_(\d)_times/ =~ id.id2name
      $2.to_i.times do
        puts $1
      end
    end
  end

  spot = Dog.new
  spot.say_woof_3_times
  spot.say_arf_2_times


Scott Raymond
http://scottraymond.net/
496ed7f402fc56b1f997855941272ad4?d=identicon&s=25 Pete Forde (Guest)
on 2006-03-19 12:17
(Received via mailing list)
> Perhaps what you want is method_missing, which can intercept any
> message that doesn't get caught by another method. For example:

That's really cool, Scott! I'll add that to my recipe book.

So is that how find_by_name_and_password() functions are built?

Also, still curious if there's a way for code in a controller function
to
find out it's function name.

Thanks for that,
Pete
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-03-19 12:17
(Received via mailing list)
On Mar 18, 2006, at 11:39 PM, Pete Forde wrote:

>> Perhaps what you want is method_missing, which can intercept any
>> message that doesn't get caught by another method. For example:
>
> That's really cool, Scott! I'll add that to my recipe book.
>
> So is that how find_by_name_and_password() functions are built?
>
> Also, still curious if there's a way for code in a controller
> function to find out it's function name.

controller.action_name

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