# Using string variable to call a method

**URL:** <https://rubytalk.org/t/using-string-variable-to-call-a-method/43582>\
**Category:** ruby-talk\
**Created:** [7 January 2008 21:14 UTC](https://rubytalk.org/t/using-string-variable-to-call-a-method/43582 "2008-01-07T21:14:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bryan\_Richardson](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Bryan\_Richardson](https://rubytalk.org/u/Bryan_Richardson)\
**Post date:** [7 January 2008 21:14 UTC](https://rubytalk.org/t/using-string-variable-to-call-a-method/43582/1 "2008-01-07T21:14:01Z")

</div>

Hello all,

How can I use a string variable as part of a method name I'm calling? For  
example say I want to call the method say\_hello using the following:

def say\_hello  
&nbsp;&nbsp;puts "Hello!"  
end

str = "hello"

say\_???

Thanks in advance!! -- BTR

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [7 January 2008 21:27 UTC](https://rubytalk.org/t/using-string-variable-to-call-a-method/43582/2 "2008-01-07T21:27:18Z")

</div>

Alle lunedì 7 gennaio 2008, Bryan Richardson ha scritto:

> Hello all,
> 
> How can I use a string variable as part of a method name I'm calling? For  
> example say I want to call the method say\_hello using the following:
> 
> def say\_hello  
> &nbsp;&nbsp;puts "Hello!"  
> end
> 
> str = "hello"
> 
> say\_???
> 
> Thanks in advance!! -- BTR

send "say\_#{str}"

Stefano

---

<div class="post-metadata">

**Author:** ![Thomas\_Wieczorek](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@Thomas\_Wieczorek](https://rubytalk.org/u/Thomas_Wieczorek)\
**Post date:** [7 January 2008 21:47 UTC](https://rubytalk.org/t/using-string-variable-to-call-a-method/43582/3 "2008-01-07T21:47:18Z")

</div>

You can use: eval, instance\_eval, class\_eval, module\_eval oder send  
The different evals are described here:  
[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513)  
(click on the "N" to read the next message in the thread)  
send is as far as I understand it, used to send messages to objects.  
Keep in mind that method calls are messages send to objects.

---

<div class="post-metadata">

**Author:** ![Daniel\_Finnie1](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@Daniel\_Finnie1](https://rubytalk.org/u/Daniel_Finnie1)\
**Post date:** [8 January 2008 00:25 UTC](https://rubytalk.org/t/using-string-variable-to-call-a-method/43582/4 "2008-01-08T00:25:30Z")

</div>

In general, the send method is preferred over evalling a string when  
possible.

In addition to send "say\_#{str}", you can do method("say\_#{str}".to\_sym)  
.call.

Dan

> **···**
>
> On 1/7/08, Thomas Wieczorek \<wieczo.yo@googlemail.com\> wrote:
> 
> > You can use: eval, instance\_eval, class\_eval, module\_eval oder send  
> > The different evals are described here:  
> > [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513)  
> > (click on the "N" to read the next message in the thread)  
> > send is as far as I understand it, used to send messages to objects.  
> > Keep in mind that method calls are messages send to objects.

---

<div class="post-metadata">

**Author:** ![Bryan\_Richardson](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Bryan\_Richardson](https://rubytalk.org/u/Bryan_Richardson)\
**Post date:** [8 January 2008 22:25 UTC](https://rubytalk.org/t/using-string-variable-to-call-a-method/43582/5 "2008-01-08T22:25:59Z")

</div>

Along these same lines, is it possible to do a require and include  
dynamically, getting the name of the file to require and module to include  
from a string?

> **···**
>
> On Jan 7, 2008 5:25 PM, Daniel Finnie \<danfinnie@optonline.net\> wrote:
> 
> > In general, the send method is preferred over evalling a string when  
> > possible.
> > 
> > In addition to send "say\_#{str}", you can do method("say\_#{str}".to\_sym)  
> > .call.
> > 
> > Dan
> > 
> > On 1/7/08, Thomas Wieczorek \<wieczo.yo@googlemail.com\> wrote:  
> > \>  
> > \> You can use: eval, instance\_eval, class\_eval, module\_eval oder send  
> > \> The different evals are described here:  
> > \> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513)  
> > \> (click on the "N" to read the next message in the thread)  
> > \> send is as far as I understand it, used to send messages to objects.  
> > \> Keep in mind that method calls are messages send to objects.  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [8 January 2008 22:47 UTC](https://rubytalk.org/t/using-string-variable-to-call-a-method/43582/6 "2008-01-08T22:47:05Z")

</div>

require is obvious, the method takes a string already.

Including a module, I think you can do:

ClassName.send(:include, Module.const\_get("ModuleName"))

or if you're working with an instance variable, replace :include with  
:extend.

Jason

> **···**
>
> On Jan 8, 2008 5:25 PM, Bryan Richardson \<btricha@gmail.com\> wrote:
> 
> > Along these same lines, is it possible to do a require and include  
> > dynamically, getting the name of the file to require and module to include  
> > from a string?
> > 
> > On Jan 7, 2008 5:25 PM, Daniel Finnie \<danfinnie@optonline.net\> wrote:
> > 
> > \> In general, the send method is preferred over evalling a string when  
> > \> possible.  
> > \>  
> > \> In addition to send "say\_#{str}", you can do method("say\_#{str}".to\_sym)  
> > \> .call.  
> > \>  
> > \> Dan  
> > \>  
> > \>  
> > \> On 1/7/08, Thomas Wieczorek \<wieczo.yo@googlemail.com\> wrote:  
> > \> \>  
> > \> \> You can use: eval, instance\_eval, class\_eval, module\_eval oder send  
> > \> \> The different evals are described here:  
> > \> \> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513)  
> > \> \> (click on the "N" to read the next message in the thread)  
> > \> \> send is as far as I understand it, used to send messages to objects.  
> > \> \> Keep in mind that method calls are messages send to objects.  
> > \> \>  
> > \> \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Bryan\_Richardson](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Bryan\_Richardson](https://rubytalk.org/u/Bryan_Richardson)\
**Post date:** [8 January 2008 23:38 UTC](https://rubytalk.org/t/using-string-variable-to-call-a-method/43582/7 "2008-01-08T23:38:09Z")

</div>

send :include, Module.const\_get(ARGV[1]) worked. Thanks!!

> **···**
>
> On Jan 8, 2008 3:47 PM, Jason Roelofs \<jameskilton@gmail.com\> wrote:
> 
> > require is obvious, the method takes a string already.
> > 
> > Including a module, I think you can do:
> > 
> > ClassName.send(:include, Module.const\_get("ModuleName"))
> > 
> > or if you're working with an instance variable, replace :include with  
> > :extend.
> > 
> > Jason
> > 
> > On Jan 8, 2008 5:25 PM, Bryan Richardson \<btricha@gmail.com\> wrote:
> > 
> > \> Along these same lines, is it possible to do a require and include  
> > \> dynamically, getting the name of the file to require and module to  
> > include  
> > \> from a string?  
> > \>  
> > \> On Jan 7, 2008 5:25 PM, Daniel Finnie \<danfinnie@optonline.net\> wrote:  
> > \>  
> > \> \> In general, the send method is preferred over evalling a string when  
> > \> \> possible.  
> > \> \>  
> > \> \> In addition to send "say\_#{str}", you can do  
> > method("say\_#{str}".to\_sym)  
> > \> \> .call.  
> > \> \>  
> > \> \> Dan  
> > \> \>  
> > \> \>  
> > \> \> On 1/7/08, Thomas Wieczorek \<wieczo.yo@googlemail.com\> wrote:  
> > \> \> \>  
> > \> \> \> You can use: eval, instance\_eval, class\_eval, module\_eval oder send  
> > \> \> \> The different evals are described here:  
> > \> \> \> [http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192513)  
> > \> \> \> (click on the "N" to read the next message in the thread)  
> > \> \> \> send is as far as I understand it, used to send messages to objects.  
> > \> \> \> Keep in mind that method calls are messages send to objects.  
> > \> \> \>  
> > \> \> \>  
> > \> \>  
> > \>
