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