Inheritence of class vars


(Ara.T.Howard) #1

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
===============================================================================


(David A. Black) #2

Hi –

am i the only one suprised by this?
[snip]

You must not have been perusing the ruby-talk archive lately :slight_smile: It’s
come up a bunch of times.

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 would say:

if you want to maintain state on a per-object basis, use instance
variables, including cases where your object is a Class object.

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

http://www.rubyist.net/~matz/slides/rc2003/mgp00020.html

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 :slight_smile:

David

···

On Sat, 14 Feb 2004, Ara.T.Howard wrote:


David A. Black
dblack@wobblini.net


(Joel VanderWerf) #3

David A. Black wrote:

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

The per-hierarchy thing was never very clear. Just which hierarchy a
variable belongs to depends on the order in which it is first assigned:

class A; end
class B < A; end

Try switching the following two lines!

class A; @@x = “A”; end
class B; @@x = “B”; end

class A; puts “In A, @@x = #{@@x}”; end
class B; puts “In B, @@x = #{@@x}”; end

Before switching the lines, there is one @@x for the hierarchy
consisting of A and B. After switching, there is one @@x for A and
another @@x for B and its descendants.

It makes sense, but it’s an easy place to get tripped.


(Florian G. Pflug) #4

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

···

On Sat, Feb 14, 2004 at 10:44:04AM +0900, David A. Black wrote:

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

(Paul Brannan) #5

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

···

On Sat, Feb 14, 2004 at 10:44:04AM +0900, David A. Black wrote:

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 :slight_smile:


(David A. Black) #6

Hi –

David A. Black wrote:

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

The per-hierarchy thing was never very clear. Just which hierarchy a
variable belongs to depends on the order in which it is first assigned:

See ruby-talk 19972 (and surrounding thread); I brought this up, and
Matz said, “I know this happens. I consider this as an error that
Ruby does not detect yet.” The funny thing is… I could have sworn that
it was fixed somewhere > 1.6, but it still seems to happen at least as
recently as 1.8.0. (Yes, I’m being lazy about testing the CVS :slight_smile:

It will become moot, though, after the new rules are implemented.

David

···

On Sat, 14 Feb 2004, Joel VanderWerf wrote:


David A. Black
dblack@wobblini.net


(David A. Black) #7

Hi –

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.

I think that’s the main difference, at least in terms of usage.
That’s what makes me have doubts about it; I’m not sure what purpose
class variables will serve except to save some typing.

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

Sure, you can do this:

class Class
@@x = 1
end

David

···

On Mon, 16 Feb 2004, Florian G. Pflug wrote:

On Sat, Feb 14, 2004 at 10:44:04AM +0900, David A. Black wrote:


David A. Black
dblack@wobblini.net