# Evaluating the string in a variable to use as a method name

**URL:** <https://www.ruby-forum.com/t/evaluating-the-string-in-a-variable-to-use-as-a-method-name/55056>\
**Category:** Ruby\
**Created:** [March 16, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/evaluating-the-string-in-a-variable-to-use-as-a-method-name/55056 "2006-03-16T18:25:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![heinous\_Ssent\_by\_Na](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@heinous\_Ssent\_by\_Na](https://www.ruby-forum.com/u/heinous_Ssent_by_Na)\
**Post date:** [March 16, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/evaluating-the-string-in-a-variable-to-use-as-a-method-name/55056/1 "2006-03-16T18:25:00Z")

</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\_Ssent\_by\_Na](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@heinous\_Ssent\_by\_Na](https://www.ruby-forum.com/u/heinous_Ssent_by_Na)\
**Post date:** [March 16, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/evaluating-the-string-in-a-variable-to-use-as-a-method-name/55056/2 "2006-03-16T18:40:32Z")

</div>

“heinous (sent by [Nabble.com](http://Nabble.com))” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> @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

You cannot use @attr, @attr is reserved for instance variables. Also,  
it’s  
a bad idea to use instance variables (@tmparray, @tmphash) as temporary  
variables.

> Where the method name is the @attr value.

It’s a one liner:

objects.sort\_by {|o| o.send(attr)}

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![heinous\_Ssent\_by\_Na](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@heinous\_Ssent\_by\_Na](https://www.ruby-forum.com/u/heinous_Ssent_by_Na)\
**Post date:** [March 16, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/evaluating-the-string-in-a-variable-to-use-as-a-method-name/55056/3 "2006-03-16T20:06:28Z")

</div>

> You cannot use @attr, @attr is reserved for instance variables. Also, it’s  
> a bad idea to use instance variables (@tmparray, @tmphash) as temporary  
> variables.

Gotcha there, I wasn’t planning to, it was more to make the variable  
more  
obvious in the example.

> It’s a one liner:  
> objects.sort\_by {|o| o.send(attr)}

Exactly what I needed, thanks… Now we’re just looking at something  
like:

def sort\_obj(objects,attr)  
objects.sort\_by {|o| o.send(attr)}  
end

---

<div class="post-metadata">

**Author:** ![heinous\_Ssent\_by\_Na](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@heinous\_Ssent\_by\_Na](https://www.ruby-forum.com/u/heinous_Ssent_by_Na)\
**Post date:** [March 17, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/evaluating-the-string-in-a-variable-to-use-as-a-method-name/55056/4 "2006-03-17T12:09:55Z")

</div>

“heinous (sent by [Nabble.com](http://Nabble.com))” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> > You cannot use @attr, @attr is reserved for instance variables. Also,  
> > it’s  
> > a bad idea to use instance variables (@tmparray, @tmphash) as temporary  
> > variables.
> 
> Gotcha there, I wasn’t planning to, it was more to make the variable more  
> obvious in the example.

It’s generally preferred to use pieces of code that are syntactically  
correct and do something. That makes everyone’s lives easier. 🙂

> > It’s a one liner:  
> > objects.sort\_by {|o| o.send(attr)}
> 
> Exactly what I needed, thanks… Now we’re just looking at something  
> like:
> 
> def sort\_obj(objects,attr)  
> objects.sort\_by {|o| o.send(attr)}  
> end

Not really worth a method IMHO bur YMMV.

Kind regards

```
robert

```

---

<div class="post-metadata">

**Author:** ![heinous\_Ssent\_by\_Na](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b5ac83/32.png) [@heinous\_Ssent\_by\_Na](https://www.ruby-forum.com/u/heinous_Ssent_by_Na)\
**Post date:** [March 17, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/evaluating-the-string-in-a-variable-to-use-as-a-method-name/55056/5 "2006-03-17T15:56:09Z")

</div>

> It’s generally preferred to use pieces of code that are syntactically  
> correct and do something. That makes everyone’s lives easier. 🙂

Thanks, I will do so in the future. Sorry about confusion that I may  
have  
caused.

> > def sort\_obj(objects,attr)  
> > objects.sort\_by {|o| o.send(attr)}  
> > end

> Not really worth a method IMHO bur YMMV.

Yeah, suspect you’re right there too, but it helps code readibility for  
me  
as I’m still learning.

Anyway, thanks again for your help, it works like a charm.
