Checking for 'nil' Class (not instance) variables?

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>
--
Posted via http://www.ruby-forum.com/.

Hi --

···

On Mon, 1 Sep 2008, John Pritchard-williams wrote:

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>

defined?(@@nosuchvar)

David

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!

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

···

--
NP: Die Ärzte - Nur einen Kuss
Jabber: sepp2k@jabber.org
ICQ: 205544826

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

···

--
Posted via http://www.ruby-forum.com/\.

Thanks !
David A. Black wrote:

//defined?(@@nosuchvar)

···

--
Posted via http://www.ruby-forum.com/.

You forgot the disclaimer that class variables are fragile and should
be avoided. :slight_smile:

Kind regards

robert

···

2008/9/1 David A. Black <dblack@rubypal.com>:

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)

--
use.inject do |as, often| as.you_can - without end

//
You forgot the disclaimer that class variables are fragile and should
be avoided. :slight_smile:
//

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 ?

···

--
Posted via http://www.ruby-forum.com/.