inheritence of class vars

A

Ara.T.Howard

am i the only one suprised by this?

~/eg/ruby > cat class_vars.rb

class A
class << self
def x= value
@@x = value
end
def x
@@x
end
end
end
class B < A; end

A.x = 'forty-two'
B.x = 42
p A.x
p B.x

class A
class << self
def x= value
@x = value
end
def x
@x
end
end
end

A.x = 'forty-two'
B.x = 42
p A.x
p B.x


~/eg/ruby > ruby class_vars.rb

42
42
"forty-two"
42

i see it, but don't believe it... is it _correct_ to say that:

iff you want a subclass to have it's own copy of a 'class var' - use instance vars

* by 'class var' i mean _not_ an instance var. obviously there are two
types

i don't know how i made it this long without that biting me, doh!

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 
F

Florian G. Pflug

Class variables, at least through 1.8.x, are essentially per-hierarchy
and not per-Class-object. For 1.9/2.0, though, Matz has presented the
following new rules:

* class variables will be local to the class/module
* if you need to access class variables from outside,
define accessors

Might be me being stupid.. but if these new rules are in place, what will
the difference between class variables and instance variables (of class
objects) be?

The only thing coming to my mind, is that class variables are easily
accessed from instance methods, while you need to do
"self.class.class_eval{@my_class_instance_var}" for class-object instance
variables.

BTW: Just for the sake of being curious... is it possible to define
class-variables of the class Class?

greetings, Florian Pflug
 
P

Paul Brannan

Personally I have mixed emotions about this. There's been so much
confusion over the years about class variables vs. instance variables
of Class objects, when the two are completely unrelated, and I suspect
that making the two things resemble each other more closely will lead
to more confusion. (This confusion mainly takes the form, I've
observed, of clouding people's perception of the simple and elegant
instance variable model.)

Then again, I don't know any way around it except to get rid of class
variables, which I'm not convinced would be a bad idea (but I know it
isn't going to happen :)

I think making class variables an alias for class instance variables
would be one alternate solution (they would just be a shortcut for
accessing the class instance variable from inside instance methods).

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,799
Messages
2,569,652
Members
45,385
Latest member
ZapGuardianReviews
Top