# Calling Method

**URL:** <https://www.ruby-forum.com/t/calling-method/97390>\
**Category:** Rails\
**Created:** [July 6, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/calling-method/97390 "2006-07-06T20:05:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [July 6, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/calling-method/97390/1 "2006-07-06T20:05:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [July 6, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/calling-method/97390/2 "2006-07-06T20:20:05Z")

</div>

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](http://ruby-doc.org/docs/ProgrammingRuby/html/ospace.html)

Find the section  
Calling Methods Dynamically

HTH

---

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [July 6, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/calling-method/97390/3 "2006-07-06T20:58:43Z")

</div>

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.  
> [Programming Ruby: The Pragmatic Programmer's Guide](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.

---

<div class="post-metadata">

**Author:** ![stimpy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@stimpy](https://www.ruby-forum.com/u/stimpy)\
**Post date:** [July 6, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/calling-method/97390/4 "2006-07-06T20:22:57Z")

</div>

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
