From: Rick DeNatale Date: 2007-10-19T04:39:26+09:00 Subject: Re: Class instance variable idiom On 10/18/07, David A. Black wrote: > I'm on record as saying that class variables are the thing I dislike > most about Ruby, so I'll try to explain why. > > They break encapsulation. They are visible to a weird assortment of > objects: class C, instances of C, class D < C, instances of D... all > the same variable. This cross-section of objects doesn't have anything > in common except that they can all see each other's class variables. And even that is squirrely. Visibility depends on sequence of appearance, watch carefully, my fingers will ever leave my hands! > class A @@x = "A" def self.show_x @@x end end A.show_x # => "A" class B < A end B.show_x # => "A" class B @@x = "B" # !> already initialized class variable @@x end B.show_x # => "B" A.show_x # => "B" class C end class D < C @@x = "D" def self.show_x @@x # !> class variable @@x of C is overridden by D end end D.show_x # => "D" class C @@x = "C" def self.show_x @@x end end D.show_x # => "D" C.show_x # => "C" class D @@x = "Blorp" # !> class variable @@x of C is overridden by D end D.show_x # => "Blorp" C.show_x # => "C" > That sums up my views. Just so you know: I love Ruby madly :-) This is > one of the very few areas where I dissent strongly from the way it's > designed. Amen, brother! -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/