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
> 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/
> 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
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
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 On Mar 18, 2006, at 11:00 PM, Pete Forde wrote:> 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails