# Using a variable as a method name?

**URL:** <https://www.ruby-forum.com/t/using-a-variable-as-a-method-name/130001>\
**Category:** Ruby\
**Created:** [February 24, 2008, 2:19am UTC](https://www.ruby-forum.com/t/using-a-variable-as-a-method-name/130001 "2008-02-24T02:19:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 24, 2008, 2:19am UTC](https://www.ruby-forum.com/t/using-a-variable-as-a-method-name/130001/1 "2008-02-24T02:19:09Z")

</div>

Hello,

I need a bit of help understanding how to use a  
variable as a name for a method call. Forgive me  
trying to explains this, I am new at all this. In the  
code listed, I am tring to set the debugging level of  
the Logger class, however, the variable in question is  
not being sustituted prior to interpreting the method  
call.

when “–debug”  
if($DEBUG\_LEVELS.include?(arg))  
mylog.level = Logger::arg  
^^^^^^^^^^^  
else  
puts “Debug level can be one of: DEBUG, INFO,  
WARN, or ERROR”  
dis\_help  
end

This causes Ruby to spit out that there is no method  
called arg which is correct. So how do I get arg to  
be substituted to the value rather than the var name?  
I tried dereferencing and using the it within a string  
(okay, a dumb idea). Any way, any help will be much  
appreciated!

TIA!  
Phy

```
  ____________________________________________________________________________________

```

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
[http://tools.search.yahoo.com/newsearch/category.php?category=shopping](http://tools.search.yahoo.com/newsearch/category.php?category=shopping)

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 24, 2008, 2:24am UTC](https://www.ruby-forum.com/t/using-a-variable-as-a-method-name/130001/2 "2008-02-24T02:24:25Z")

</div>

Try Logger.send(arg)

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 24, 2008, 2:36am UTC](https://www.ruby-forum.com/t/using-a-variable-as-a-method-name/130001/3 "2008-02-24T02:36:43Z")

</div>

On Sat, Feb 23, 2008 at 7:18 PM, Phy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This causes Ruby to spit out that there is no method  
> called arg which is correct. So how do I get arg to  
> be substituted to the value rather than the var name?  
> I tried dereferencing and using the it within a string  
> (okay, a dumb idea). Any way, any help will be much  
> appreciated!

What is the /type/ of arg?

If it’s a Proc, you might try arg.call().

If you’re trying to do it via string, look into eval.

Jay Fields has some thoughts:

> **[Ruby eval with a binding](http://blog.jayfields.com/2006/07/ruby-eval-with-binding.html)**
>
> I recently discussed how you can evaluate blocks in different scopes by using instance\_eval, yield, and block.call. In this entry I'll sho...

> **[Ruby Block Scope](http://blog.jayfields.com/2006/07/ruby-block-scope.html)**
>
> While doing DSL work you constantly evaluate blocks (or code as strings) in various contexts . In fact, one key to using a DSL is the abili...

I seem to remember seeing a better reference, only I can’t recall by  
whom or  
where.

Regards,  
Sam

---

<div class="post-metadata">

**Author:** ![Phy\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a587f6/32.png) [@Phy\_P](https://www.ruby-forum.com/u/Phy_P)\
**Post date:** [February 24, 2008, 2:46am UTC](https://www.ruby-forum.com/t/using-a-variable-as-a-method-name/130001/4 "2008-02-24T02:46:43Z")

</div>

Hi,

On Sun, Feb 24, 2008 at 12:23 PM, Corey H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Try Logger.send(arg)

In the case of Rails, `arg’ should also be lower case. Is it Logger.warn  
or  
.warning?

Logger.send(arg.downcase)

Arlen
