From: zdennis Date: 2006-04-11T01:53:11+09:00 Subject: Re: Adding in class attribute with a Module -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 dblack@wobblini.net wrote: > Hi -- > > On Tue, 11 Apr 2006, zdennis wrote: > >> James Britt wrote: > > > Actually I wrote this bit :-) > >>> >>>> I don't like the sprawl of the concept of >>>> an "attribute" of a class-hierarchy-plus-instances-of-those-classes. >> >> >> I think this is what you are referring to David, and I think it is >> strange to: >> >> zdennis@lima:~$ irb >> irb(main):001:0> class A >> irb(main):002:1> @@myvar = 5 >> irb(main):003:1> class << self ; @myvar = 10 ; end >> irb(main):004:1> end >> => 10 >> irb(main):005:0> A.instance_variables >> => [] >> irb(main):006:0> A.class_variables >> => ["@@myvar"] >> irb(main):007:0> a = class A ; class << self ; self ; end ; end >> => # >> irb(main):008:0> a.instance_variables >> => ["@myvar"] >> irb(main):009:0> a.instance_eval "@myvar" >> => 10 >> irb(main):010:0> A.module_eval "@@myvar" >> => 5 >> irb(main):011:0> >> >> It seems like, @@myvar and @myvar should refer to the same reference >> point. I would expect @@ to signify a class level attribute, which >> is actually an instance attribute on the class itself. In my head >> that makes sense. > > > I disagree. If @@myvar is just the instance variable @myvar of some > class object, then it would presumably have exactly the same scope as > that instance variable -- in which case, there's no reason for it to > exist. If it's visible to other objects (i.e., when 'self' is > something other than the class object), then it isn't the same as an > instance variable anyway. > > I wouldn't want to see any crossover between class variables and > instance variables. A class object's instance variables are strictly > the business of the class in its "civilian" capacity as a regular > object. Class variables, on the other hand, are a special case. > The two things are really not related conceptually at all. > I see what you are saying, if there was crossover, then anything dealing with the class as an object, in it's "civilian" state as you put it would essentially become a class variable which it is not. On a implementation note, we achieve the same end result don't we (focus on the *end* part of that statement, and nothing in between)? class C class << self ; attr_accessor :m ; end end class D def self.m ; @@m ; end def self.m=arg ; @@m = arg ; end end - From a usage perspective as the end-user of the langauge nothing changes: irb(main):025:0> C.m = 5 => 5 irb(main):026:0> C.m => 5 irb(main):027:0> D.m = 10 => 10 irb(main):028:0> D.m => 10 It definitely seems that if the two are to stay separate by implementation and conceptualization that it would nice to have those class accessor methods, which don't invade the class's civilian state. I use that as a shortcut to writing my own accessors... class << self ; attr_* :foo ; end Saves me typing in light of a class get/set shortcut existing... Zach -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.1 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFEOo6CMyx0fW1d8G0RArdFAJ474mMynkv3EZSr0NHPOs7o4BHVLACeK+QZ 6UMsY80gJKd/fK+zBB/yzH0= =d/7i -----END PGP SIGNATURE-----