PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Using a variable as a method name?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Using a variable as a method name?

Réponse
 
LinkBack Outils de la discussion
Vieux 24/02/2008, 01h18   #1
Phy Prabab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Using a variable as a method name?

Hello,

I need a bit of 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_
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 will be much
appreciated!

TIA!
Phy


__________________________________________________ __________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsea...egory=shopping

  Réponse avec citation
Vieux 24/02/2008, 01h23   #2
Corey Haines
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using a variable as a method name?

[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 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_
> 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 will be much
> appreciated!
>
> TIA!
> Phy
>
>
>
> __________________________________________________ __________________________________
> Looking for last minute shopping deals?
> Find them fast with Yahoo! Search.
> http://tools.search.yahoo.com/newsea...egory=shopping
>
>



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

  Réponse avec citation
Vieux 24/02/2008, 01h35   #3
Sam Larbi
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using a variable as a method name?

[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 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/ru...h-binding.html
http://blog.jayfields.com/2006/07/ruby-block-scope.html

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

Regards,
Sam

  Réponse avec citation
Vieux 24/02/2008, 01h45   #4
Arlen Cuss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using a variable as a method name?

[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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 18h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 1,23839 seconds with 12 queries