Christopher J. Bottaro
2006-Nov-16 21:21 UTC
dynamically adding static (class) methods to a class
Hello again, I guess this is more of a Ruby question, but here goes anyways... I want to intercept all calls to non existent class methods and then call an existing class method with the non existent name. In code... class MyModel < ActiveRecord::Base def MyModel.method_missing(symbol, *args) MyModel.func(symbol.id2name) end def MyModel.func(func_called) puts "You called #{func_called}" end end When I try this code, it gets lost in recursion. Thanks for the help. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor
2006-Nov-16 21:27 UTC
Re: dynamically adding static (class) methods to a class
Please post to the comp.lang.ruby. Thank you, -Conrad On 11/16/06, Christopher J. Bottaro <cjbottaro-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello again, > > I guess this is more of a Ruby question, but here goes anyways... > > I want to intercept all calls to non existent class methods and then > call an existing class method with the non existent name. > > In code... > > class MyModel < ActiveRecord::Base > > def MyModel.method_missing(symbol, *args) > MyModel.func(symbol.id2name) > end > > def MyModel.func(func_called) > puts "You called #{func_called}" > end > > end > > When I try this code, it gets lost in recursion. > > Thanks for the help. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christopher J. Bottaro
2006-Nov-16 22:50 UTC
Re: dynamically adding static (class) methods to a class
Actually, it turned out to be a Rails problem... The code I originally wrote below actually works. The problem was in my real code where I was calling MyModel.find_by_name from within method_missing... which was causing infinite recursion because Rails uses method_missing to implement all the find_by_* methods. -- C Conrad Taylor wrote:> Please post to the comp.lang.ruby. > > Thank you, > > -Conrad > > > On 11/16/06, Christopher J. Bottaro <cjbottaro-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello again, > > > > I guess this is more of a Ruby question, but here goes anyways... > > > > I want to intercept all calls to non existent class methods and then > > call an existing class method with the non existent name. > > > > In code... > > > > class MyModel < ActiveRecord::Base > > > > def MyModel.method_missing(symbol, *args) > > MyModel.func(symbol.id2name) > > end > > > > def MyModel.func(func_called) > > puts "You called #{func_called}" > > end > > > > end > > > > When I try this code, it gets lost in recursion. > > > > Thanks for the help.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---