# Invoking a method by name

**URL:** <https://www.ruby-forum.com/t/invoking-a-method-by-name/90202>\
**Category:** Rails\
**Created:** [March 26, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/invoking-a-method-by-name/90202 "2007-03-26T20:27:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [March 26, 2007, 8:27pm UTC](https://www.ruby-forum.com/t/invoking-a-method-by-name/90202/1 "2007-03-26T20:27:19Z")

</div>

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}

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [March 26, 2007, 8:33pm UTC](https://www.ruby-forum.com/t/invoking-a-method-by-name/90202/2 "2007-03-26T20:33:20Z")

</div>

> 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](http://www.ruby-doc.org/core/classes/Object.html#M000334)

---

<div class="post-metadata">

**Author:** ![rlwinter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecccb3/32.png) [@rlwinter](https://www.ruby-forum.com/u/rlwinter)\
**Post date:** [March 26, 2007, 8:41pm UTC](https://www.ruby-forum.com/t/invoking-a-method-by-name/90202/3 "2007-03-26T20:41:21Z")

</div>

Philip,

Excellent, that’s just what I was looking for.
