Hello all, I have a need to invoke methods dynamically by name. Say I have a method like: def invoke_method_with_name(method_name) person = Person.find(params[:id]) method_to_invoke = ~~ some code to find the method from a string containing the method name ~~ person.method_to_invoke end I do this in Objective C with something like the following: SEL setWidthHeight; setWidthHeight = @selector(setWidth:height:); or setWidthHeight = NSSelectorFromString(aBuffer); Where SEL is a special type representing an Objective C method signature, and the @selector directive or NSSelectorFromString(aBuffer) is used to find the method. I have a feeling that I''m making this more complicated than need be. Is it possible to do something as simple as: person.#{method_to_invoke} --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip, Excellent, that''s just what I was looking for. On Mar 26, 2:43 pm, Philip Hallstrom <r...-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> wrote:> > I have a need to invoke methods dynamically by name. > > > Say I have a method like: > > > def invoke_method_with_name(method_name) > > person = Person.find(params[:id]) > > method_to_invoke = ~~ some code to find the method from a string > > containing the method name ~~ > > person.method_to_invoke > > end > > > I do this in Objective C with something like the following: > > > SEL setWidthHeight; > > setWidthHeight = @selector(setWidth:height:); > > or > > setWidthHeight = NSSelectorFromString(aBuffer); > > > Where SEL is a special type representing an Objective C method > > signature, and the @selector directive or > > NSSelectorFromString(aBuffer) is used to find the method. > > > I have a feeling that I''m making this more complicated than need be. > > > Is it possible to do something as simple as: > > > person.#{method_to_invoke} > > You want send()... > > http://www.ruby-doc.org/core/classes/Object.html#M000334--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> I have a need to invoke methods dynamically by name. > > Say I have a method like: > > def invoke_method_with_name(method_name) > person = Person.find(params[:id]) > method_to_invoke = ~~ some code to find the method from a string > containing the method name ~~ > person.method_to_invoke > end > > I do this in Objective C with something like the following: > > SEL setWidthHeight; > setWidthHeight = @selector(setWidth:height:); > or > setWidthHeight = NSSelectorFromString(aBuffer); > > Where SEL is a special type representing an Objective C method > signature, and the @selector directive or > NSSelectorFromString(aBuffer) is used to find the method. > > I have a feeling that I''m making this more complicated than need be. > > Is it possible to do something as simple as: > > person.#{method_to_invoke}You want send()... http://www.ruby-doc.org/core/classes/Object.html#M000334 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---