# Passing function names from string variables

**URL:** <https://www.ruby-forum.com/t/passing-function-names-from-string-variables/55055>\
**Category:** Rails\
**Created:** [March 16, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/passing-function-names-from-string-variables/55055 "2006-03-16T18:21:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 16, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/passing-function-names-from-string-variables/55055/1 "2006-03-16T18:21:52Z")

</div>

This may be a more generic Ruby question, so I’m going to ask on the  
Ruby  
forum, but I’m trying to figure out if there is a way to pass in the  
string  
value of a variable as the name of a method.

For example, I would like to do something like:

def sort\_obj\_by\_uid(objects,@attr)  
@tmparray = Array.new  
@tmphash = Hash.new  
for object in @objects  
if ! @tmphash.has\_key?(removed\_email\_address@domain.invalid\_s)  
@tmphash[removed\_email\_address@domain.invalid\_s] = Array.new  
end  
@tmphash[removed\_email\_address@domain.invalid\_s].push(object)  
end  
end

Where the method name is the @attr value.

---

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 16, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/passing-function-names-from-string-variables/55055/2 "2006-03-16T18:25:00Z")

</div>

Hi –

On Thu, 16 Mar 2006, Leah C. wrote:

> ```
> if ! @tmphash.has_key?(removed_email_address@domain.invalid_s)
> @tmphash[removed_email_address@domain.invalid_s] = Array.new
> end
> @tmphash[removed_email_address@domain.invalid_s].push(object)
> 
> ```
> 
> end  
> end
> 
> Where the method name is the @attr value.

Yes: send, like this:

object.send(@attr).to\_s

If you want to send additional arguments, you put them in the second,  
third… positions.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 16, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/passing-function-names-from-string-variables/55055/3 "2006-03-16T18:25:00Z")

</div>

Very easy:

@obj.send(@attr.to\_sym)

---

<div class="post-metadata">

**Author:** ![heinous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e95f7d/32.png) [@heinous](https://www.ruby-forum.com/u/heinous)\
**Post date:** [March 16, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/passing-function-names-from-string-variables/55055/4 "2006-03-16T18:28:04Z")

</div>

On Thu, 16 Mar 2006, removed\_email\_address@domain.invalid wrote:

> > def sort\_obj\_by\_uid(objects,@attr)  
> > Where the method name is the @attr value.
> 
> Yes: send, like this:
> 
> object.send(@attr).to\_s

Whoops, I meant:

object.send(@attr.to\_s)

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” chapters now available  
from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)
