# Calling a method whose name I only know at run-time

**URL:** <https://www.ruby-forum.com/t/calling-a-method-whose-name-i-only-know-at-run-time/84344>\
**Category:** Ruby\
**Created:** [February 3, 2007, 11:52am UTC](https://www.ruby-forum.com/t/calling-a-method-whose-name-i-only-know-at-run-time/84344 "2007-02-03T11:52:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [February 3, 2007, 11:52am UTC](https://www.ruby-forum.com/t/calling-a-method-whose-name-i-only-know-at-run-time/84344/1 "2007-02-03T11:52:50Z")

</div>

Hi folks,

I have a handful of methods that perform operations on chunks of data.  
Which of these methods is called is specified in a YAML configuration so  
I only know the method names at run-time, which are, of course, strings.  
I am familiar with eval but would like to avoid using it to keep my code  
safe. Is there another way I can do it?

Thanks,

Chad.

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [February 3, 2007, 12:04pm UTC](https://www.ruby-forum.com/t/calling-a-method-whose-name-i-only-know-at-run-time/84344/2 "2007-02-03T12:04:02Z")

</div>

Oops, solved it. Sorry, I missed Object#method the first time around.  
8)

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [February 3, 2007, 12:24pm UTC](https://www.ruby-forum.com/t/calling-a-method-whose-name-i-only-know-at-run-time/84344/3 "2007-02-03T12:24:26Z")

</div>

On 2/3/07, Chad T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Oops, solved it. Sorry, I missed Object#method the first time around.  
> 8)

And there is the Object#send that will do what you want, i.e.  
any\_object.send(“method1”, arg1, arg2)

or

any\_object.send(“method1”, \*args)

(if send insists on symbols for method names, use String#to\_sym)

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [February 3, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/calling-a-method-whose-name-i-only-know-at-run-time/84344/4 "2007-02-03T14:13:32Z")

</div>

Hi –

On Sat, 3 Feb 2007, Jan S. wrote:

> (if send insists on symbols for method names, use String#to\_sym)

It doesn’t; you can use either.

David

---

<div class="post-metadata">

**Author:** ![builderchad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9e8a1a/32.png) [@builderchad](https://www.ruby-forum.com/u/builderchad)\
**Post date:** [February 3, 2007, 7:23pm UTC](https://www.ruby-forum.com/t/calling-a-method-whose-name-i-only-know-at-run-time/84344/5 "2007-02-03T19:23:07Z")

</div>

It looks like send is actually more appropriate for my needs, thanks,  
I’ll give it a go.
