Testing global variables for existence?

Hi,

I need to set a global variable (e.g. $configfile ) to a default value
if it has not been set before to anything else. In order to
not overwrite it I have to check whether it has been set previously.

$configfile.nil? would do the job, but ruby -w spits out a warning
that the variable has not yet been initialized.

How do I test whether the variable has been set without causing
ruby -w to print a warning in case it has not been set?

regards
Hadmut

How do I test whether the variable has been set without causing
ruby -w to print a warning in case it has not been set?

With defined?

uln% ruby -we '$a = 12 unless defined?($a); p $a'
12
uln%

Guy Decoux

ts wrote:

With defined?

uln% ruby -we '$a = 12 unless defined?($a); p $a'
12
uln%

Thanks! :slight_smile:

Hadmut