# Calling a method using a variable name?

**URL:** <https://www.ruby-forum.com/t/calling-a-method-using-a-variable-name/88758>\
**Category:** Ruby\
**Created:** [March 14, 2007, 2:52am UTC](https://www.ruby-forum.com/t/calling-a-method-using-a-variable-name/88758 "2007-03-14T02:52:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajtwatching](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9e5f3/32.png) [@ajtwatching](https://www.ruby-forum.com/u/ajtwatching)\
**Post date:** [March 14, 2007, 2:52am UTC](https://www.ruby-forum.com/t/calling-a-method-using-a-variable-name/88758/1 "2007-03-14T02:52:17Z")

</div>

Hey folks,

Like many posts have no doubt started… I’m giving this ruby business  
a crack!! 😉

Now this might be a really bad approach… but I’ll throw it out there.

Here’s my simple class.

class Myclass  
def initialize  
end

def hello  
puts “hello”  
end

def bye  
puts “bye”  
end  
end

As expected, works a treat.

friendly = Myclass.new  
friendly.hello  
friendly.bye

Now, given I now what methods are available, is there a way to do  
something similar to this…

methods = [‘hello’, ‘bye’]  
methods.each { |m| friendly.m }

Regards,

ajt.

---

<div class="post-metadata">

**Author:** ![ajtwatching](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9e5f3/32.png) [@ajtwatching](https://www.ruby-forum.com/u/ajtwatching)\
**Post date:** [March 14, 2007, 2:59am UTC](https://www.ruby-forum.com/t/calling-a-method-using-a-variable-name/88758/2 "2007-03-14T02:59:17Z")

</div>

ajtwatching wrote:

> def initialize
> 
> methods.each { |m| friendly.m }
> 
> Regards,
> 
> ajt.

Look up the **send** method:

obj. **send** (“methodname”, arg1, arg2)

---

<div class="post-metadata">

**Author:** ![ajtwatching](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9e5f3/32.png) [@ajtwatching](https://www.ruby-forum.com/u/ajtwatching)\
**Post date:** [March 14, 2007, 3:16am UTC](https://www.ruby-forum.com/t/calling-a-method-using-a-variable-name/88758/3 "2007-03-14T03:16:18Z")

</div>

“ajtwatching” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> def initialize
> 
> methods.each { |m| friendly.m }  
> methods.each{|m| friendly.send m}
> 
> Regards,
> 
> ajt.

–  
[http://kdr2.net](http://kdr2.net)

```
                                           ------yours Killy Draw

```

---

<div class="post-metadata">

**Author:** ![ajtwatching](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9e5f3/32.png) [@ajtwatching](https://www.ruby-forum.com/u/ajtwatching)\
**Post date:** [March 14, 2007, 10:58am UTC](https://www.ruby-forum.com/t/calling-a-method-using-a-variable-name/88758/4 "2007-03-14T10:58:07Z")

</div>

On 3/13/07, Timothy H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Look up the **send** method:
> 
> obj. **send** (“methodname”, arg1, arg2)

Or just send which in most cases is preferable. They both do the same  
thing (in fact I believe that one is an alias of the other). **send**  
is there to cover cases where a class defines a send method for  
another purpose.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![ajtwatching](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b9e5f3/32.png) [@ajtwatching](https://www.ruby-forum.com/u/ajtwatching)\
**Post date:** [March 14, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/calling-a-method-using-a-variable-name/88758/5 "2007-03-14T21:36:14Z")

</div>

Yep, +1. Use #send() rather than # **send** ().

Example from irb:

> > “muppet”.send(:to\_s)  
> > =\> “muppet”

You’re telling the String object “muppet” to receive the message  
“to\_s”, in a Smalltalk sense, or, in a Java sense, you’re telling the  
String object “muppet” to call the method “to\_s”.

To do it with a variable:

> > kermit = “to\_s”  
> > =\> “to\_s”  
> > “muppet”.send(kermit)  
> > =\> “muppet”

Badda bing badda boom.

–  
Giles B.  
[http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)

> **[Giles Bowkett](http://gilesbowkett.blogspot.com)**
>
> half-disavowed old blog. new blog at gilesbowkett.com

[http://giles.tumblr.com/](http://giles.tumblr.com/)
