Is there a way to call a method if I only have a string that contains the method name. For example: @methods = [''method1'', ''method2'', ''method3''] @step = 1 # I want to call method2 which has a definition in this class -- Posted via http://www.ruby-forum.com/.
Ryan Belcher wrote:> Is there a way to call a method if I only have a string that contains > the method name. > > For example: > > @methods = [''method1'', ''method2'', ''method3''] > @step = 1 > > # I want to call method2 which has a definition in this classLook here. http://ruby-doc.org/docs/ProgrammingRuby/html/ospace.html Find the section Calling Methods Dynamically HTH -- Posted via http://www.ruby-forum.com/.
Hi Ryan, Ryan Belcher wrote:> Is there a way to call a method if I only have > a string that contains the method name. > > For example: > > @methods = [''method1'', ''method2'', ''method3'']Not sure what you mean by "call" but if, for example, you were trying to redirect_to I think... redirect_to :action => eval(@methods[1]) might do what you want. hth, Bill
Ajaya Agrawalla wrote:> Ryan Belcher wrote: >> Is there a way to call a method if I only have a string that contains >> the method name. >> >> For example: >> >> @methods = [''method1'', ''method2'', ''method3''] >> @step = 1 >> >> # I want to call method2 which has a definition in this class > > Look here. > http://ruby-doc.org/docs/ProgrammingRuby/html/ospace.html > > Find the section > Calling Methods Dynamically > > HTHThanks for the pointer. self.send(@methods[@step]) worked. -- Posted via http://www.ruby-forum.com/.