[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using a variable as a method name?

Phy Prabab

2/24/2008 1:18:00 AM

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?categor...

3 Answers

Corey Haines

2/24/2008 1:24:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Try Logger.send(arg)

On Sat, Feb 23, 2008 at 8:18 PM, Phy Prabab <phyprabab@yahoo.com> wrote:

> 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?categor...
>
>


--
http://www.corey...
The Internet's Premiere source of information about Corey Haines

Sammy Larbi

2/24/2008 1:36:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On Sat, Feb 23, 2008 at 7:18 PM, Phy Prabab <phyprabab@yahoo.com> 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:
http://blog.jayfields.com/2006/07/ruby-eval-with-bi...
http://blog.jayfields.com/2006/07/ruby-block-...

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

Regards,
Sam

Arlen Cuss

2/24/2008 1:46:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

Hi,

On Sun, Feb 24, 2008 at 12:23 PM, Corey Haines <coreyhaines@gmail.com>
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