Calling Method

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

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

HTH

Ajaya A. 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

HTH

Thanks for the pointer.

self.send(@methods[@step]) worked.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs