See below, what’s the proper way of checking for initialized
class-variables ?
Cheers
John
irb(main):031:0> @nosuchvar.nil?
=> true
irb(main):032:0> @@nosuchvar.nil?
NameError: uninitialized class variable @@nosuchvar in Object
from (irb):32
irb(main):033:0>
Hi –
On Mon, 1 Sep 2008, John Pritchard-williams wrote:
NameError: uninitialized class variable @@nosuchvar in Object
from (irb):32
irb(main):033:0>
defined?(@@nosuchvar)
David
Thanks !
David A. Black wrote:
//defined?(@@nosuchvar)
John Pritchard-williams wrote:
See below, what’s the proper way of checking for initialized
class-variables ?
[…]
irb(main):031:0> @nosuchvar.nil?
=> true
That doesn’t check whether @nosuchvar was initialized. Look:
defined?(@var)
=> nil
@var.nil?
=> true
@var = nil # @var is now initialized
=> nil
@var.nil?
=> true
defined?(@var)
=> “instance-variable”
HTH,
Sebastian
//
You forgot the disclaimer that class variables are fragile and should
be avoided. 
//
Nah, I’ll check each one with:
fragile?(@@var)
(that’s just a bad joke)…but seriously : ‘fragile’ in implementation ?
or ‘fragile’ from a high-level/idealistic point of view ?
irb(main):031:0> @nosuchvar.nil?
=> true
irb(main):032:0> @@nosuchvar.nil?
NameError: uninitialized class variable @@nosuchvar in Object
from (irb):32
irb(main):033:0>
Well, to query about class variables/methods, I would prefer to use
class methods.
use self.class to get the class object of current instance, then query
the class variables of this class using class_variable_defined? method.
irb(main):026:0> self.class.class_variable_defined? :@@nosuchvar
=> false
2008/9/1 David A. Black [email protected]:
On Mon, 1 Sep 2008, John Pritchard-williams wrote:
See below, what’s the proper way of checking for initialized
class-variables ?
–
irb(main):031:0> @nosuchvar.nil?
=> true
irb(main):032:0> @@nosuchvar.nil?
NameError: uninitialized class variable @@nosuchvar in Object
from (irb):32
irb(main):033:0>
defined?(@@nosuchvar)
You forgot the disclaimer that class variables are fragile and should
be avoided. 
Kind regards
robert