# Variables to call methods in ruby

**URL:** <https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661>\
**Category:** Ruby\
**Created:** [June 25, 2007, 12:43am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661 "2007-06-25T00:43:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harnish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@Harnish](https://www.ruby-forum.com/u/Harnish)\
**Post date:** [June 25, 2007, 12:43am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661/1 "2007-06-25T00:43:35Z")

</div>

Hi,

This is Harnish. I was just wondering if I could call methods using  
variables?

As for example:

* * *

testfunc = “func”

class Test  
def self.func  
puts “hello there…!”  
end  
end

## Test.

Essentially I want to call Test.func; but “func” lies in the variable  
testfunc; is it possible for me to invoke Test.func using variable  
testfunc? If yes, how? Any ideas, greatly appreciated.

Regards,  
Harnish

---

<div class="post-metadata">

**Author:** ![Harnish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@Harnish](https://www.ruby-forum.com/u/Harnish)\
**Post date:** [June 25, 2007, 12:49am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661/2 "2007-06-25T00:49:09Z")

</div>

On 6/24/07, Harnish [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> testfunc; is it possible for me to invoke Test.func using variable  
> testfunc? If yes, how? Any ideas, greatly appreciated.

You can use send:

testfunc = “func”

class Test  
def self.func  
puts “hello there…!”  
end  
end

Test.send testfunc

---

<div class="post-metadata">

**Author:** ![Harnish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@Harnish](https://www.ruby-forum.com/u/Harnish)\
**Post date:** [June 25, 2007, 12:50am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661/3 "2007-06-25T00:50:10Z")

</div>

Harnish wrote:

> Essentially I want to call Test.func; but “func” lies in the variable  
> testfunc; is it possible for me to invoke Test.func using variable  
> testfunc? If yes, how? Any ideas, greatly appreciated.

Check out the “ **send** ” method:

var = “func”  
obj. **send** (var, args)

---

<div class="post-metadata">

**Author:** ![Harnish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@Harnish](https://www.ruby-forum.com/u/Harnish)\
**Post date:** [June 25, 2007, 1:51am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661/4 "2007-06-25T01:51:14Z")

</div>

On Jun 24, 5:48 pm, Tim H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Harnish wrote:
> 
> > Essentially I want to call Test.func; but “func” lies in the variable  
> > testfunc; is it possible for me to invoke Test.func using variable  
> > testfunc? If yes, how? Any ideas, greatly appreciated.
> 
> Check out the “ **send** ” method:
> 
> var = “func”  
> obj. **send** (var, args)

I’ve always used it without the underscores:

var = “func”  
obj.send “func”, an\_arg

---

<div class="post-metadata">

**Author:** ![Harnish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@Harnish](https://www.ruby-forum.com/u/Harnish)\
**Post date:** [June 25, 2007, 1:58am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661/5 "2007-06-25T01:58:15Z")

</div>

On 6/25/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’ve always used it without the underscores:

You can, but in case you’re wondering the whys and wherefores of all  
this… it’s possible to easily change the “send” method on a class  
with anyone noticing… this could cause "Bad Things"™ to happen.

It’s possible to redefine **send** too, but a warning is given and  
it’s generally considered bad form to redefine, delete, or otherwise  
modify methods using the **x** naming convention. You’ll notice  
classes like BlankSlate (  
[http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc](http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc) ) take this  
into account:

class BlankSlate  
instance\_methods.each { |m| undef\_method m unless m =~ /^\_\_/ }  
end

Cheers,  
Peter C.

> **[Ruby Inside: The Ruby Blog](http://www.rubyinside.com/)**
>
> The biggest Ruby and Rails blog since 2006 - tutorials, news, and lots more

---

<div class="post-metadata">

**Author:** ![Harnish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@Harnish](https://www.ruby-forum.com/u/Harnish)\
**Post date:** [June 25, 2007, 2:46am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661/6 "2007-06-25T02:46:23Z")

</div>

On 6/24/07, Peter C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > >
> 
> it’s generally considered bad form to redefine, delete, or otherwise  
> modify methods using the **x** naming convention. You’ll notice  
> classes like BlankSlate (  
> [http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc](http://onestepback.org/index.cgi/Tech/Ruby/BlankSlate.rdoc) ) take this  
> into account:
> 
> class BlankSlate  
> instance\_methods.each { |m| undef\_method m unless m =~ /^\_\_/ }  
> end

I see your point, it just seems like using **send** breaks  
encapsulation.  
If a class is re-defining send for some reason, the user probably  
shouldn’t  
have to know/care about it.

I suppose if the proxy is adding some extra methods ( is\_proxy?() ) then  
**send** would be a better choice. But then again, unless you add that  
to  
everything, you’d have to use a respond\_to? – starting to get pretty  
ugly.  
Can you think of any cases that show why always using **send** is both  
necessary and clean?

And of course in the simple proxy example, using **send** doesn’t really  
make a difference.

class BlankSlate  
instance\_methods.each { |m| undef\_method m unless m =~ /^\_\_/ }  
end

class Proxy \< BlankSlate  
def initialize obj  
@obj = obj  
end  
def method\_missing msg , \*args , &block  
puts “proxying #{msg}…”  
@obj.send msg , \*args , &block  
end  
end

str = Proxy.new “hello”  
puts ( str.send(:size) == str. **send** (:size)) # =\> true

-Adam

---

<div class="post-metadata">

**Author:** ![Harnish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@Harnish](https://www.ruby-forum.com/u/Harnish)\
**Post date:** [June 25, 2007, 3:42am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661/7 "2007-06-25T03:42:26Z")

</div>

Hi –

On Mon, 25 Jun 2007, Adam B. wrote:

> I see your point, it just seems like using **send** breaks encapsulation.  
> If a class is re-defining send for some reason, the user probably shouldn’t  
> have to know/care about it.
> 
> I suppose if the proxy is adding some extra methods ( is\_proxy?() ) then  
> **send** would be a better choice. But then again, unless you add that to  
> everything, you’d have to use a respond\_to? – starting to get pretty ugly.  
> Can you think of any cases that show why always using **send** is both  
> necessary and clean?

It’s really more for cases where ‘send’ is used for something totally  
different:

class Message  
def send  
Net::SMTP.open …  
…  
end  
end

If someone wants to send (in the original sense) a message (in the  
original sense 🙂 to a Message object, they’ll end up calling the new  
send.

David

---

<div class="post-metadata">

**Author:** ![Harnish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@Harnish](https://www.ruby-forum.com/u/Harnish)\
**Post date:** [June 25, 2007, 4:11am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661/8 "2007-06-25T04:11:28Z")

</div>

On 6/24/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If someone wants to send (in the original sense) a message (in the  
> original sense 🙂 to a Message object, they’ll end up calling the new  
> send.

Doh! Good call. Time to kick my double underscore aversion 😉

-Adam

---

<div class="post-metadata">

**Author:** ![Harnish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bc79bd/32.png) [@Harnish](https://www.ruby-forum.com/u/Harnish)\
**Post date:** [June 25, 2007, 4:13am UTC](https://www.ruby-forum.com/t/variables-to-call-methods-in-ruby/107661/9 "2007-06-25T04:13:28Z")

</div>

On 6/25/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mon, 25 Jun 2007, Adam B. wrote:
> 
> > Can you think of any cases that show why always using **send** is both  
> > necessary and clean?
> 
> It’s really more for cases where ‘send’ is used for something totally  
> different:

David illustrates it better than I did, but the basic “rule” I tend to  
take away is that “ **send** ” is, effectively, guaranteed to do what I  
think it will (I’ll see a warning if it doesn’t)… whereas “send”  
might not. Such concerns aren’t that important if you’re working on  
classes you’re familiar with, however, or if methods aren’t being  
called on dynamically-chosen classes you have little control over.

Cheers,  
Peter C.

> **[Ruby Inside: The Ruby Blog](http://www.rubyinside.com/)**
>
> The biggest Ruby and Rails blog since 2006 - tutorials, news, and lots more
