# Need cohercing a var into a method name

**URL:** <https://www.ruby-forum.com/t/need-cohercing-a-var-into-a-method-name/129948>\
**Category:** Ruby\
**Created:** [February 22, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/need-cohercing-a-var-into-a-method-name/129948 "2008-02-22T23:14:55Z")\
**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 22, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/need-cohercing-a-var-into-a-method-name/129948/1 "2008-02-22T23:14:55Z")

</div>

Hello,

I need a small bit of help, I am playing with Logger class and ran into  
a small problem I am not sure how to solve. Namely, I was interested in  
command line setting of the logging level, however, the passed argument  
is not accepted as a method name nor can I figure out how to get the arg  
“coherced” to a string prior to evaluation of the method call. Here is  
my code:

when “–debug”  
if(DEBUG\_LEVELS.include?(arg))  
mylog.level = Logger::arg.to\_s  
^^^^^^^^^^^^  
else  
puts " “Debug level can be one of DEBUG, INFO, WARN, ERROR, or  
FATAL.”  
…  
(hi-lited is the code segment in question)  
I am using GetoptLong to do the cmd line processing. The error given by  
ruby is:  
test.rb:136 undefined method `arg’ for Logger:Class (NoMethodError)  
…

TIA,  
Phy

```
  ____________________________________________________________________________________

```

Never miss a thing. Make Yahoo your home page.  
[http://www.yahoo.com/r/hs](http://www.yahoo.com/r/hs)

---

<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 22, 2008, 11:29pm UTC](https://www.ruby-forum.com/t/need-cohercing-a-var-into-a-method-name/129948/2 "2008-02-22T23:29:38Z")

</div>

On Feb 22, 2008, at 5:14 PM, Phy P. wrote:

> ```
> if(DEBUG_LEVELS.include?(arg))
> 
> ```
> 
> …
> 
> TIA,  
> Phy

Logger.const\_get(arg)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<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 25, 2008, 7:44pm UTC](https://www.ruby-forum.com/t/need-cohercing-a-var-into-a-method-name/129948/3 "2008-02-25T19:44:51Z")

</div>

Rob,

Thanks for the method. Appreciate the quick and  
concise help!

Phy

— Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> name nor can I  
> INFO, WARN, ERROR,
> 
> > TIA,

* * *

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 22, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/need-cohercing-a-var-into-a-method-name/129948/4 "2008-02-22T23:45:29Z")

</div>

On 2/22/08, Phy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> .....
> 
> ```

Logger#level actually takes an integer. Maybe something like this:

if Logger::Severity.const\_defined?(arg)  
mylog.level = Logger::Severity.const\_get(arg)  
else  
puts “Debug level must be one of  
#{Logger.Severity.constants.join(‘,’).”  
end

–  
Rick DeNatale

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