From: dblack@... Date: 2006-04-11T01:35:56+09:00 Subject: Re: Adding in class attribute with a Module 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. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" coming in PDF April 15, and in paper May 1! http://www.manning.com/black