From: Tony Mobily Date: 2006-02-20T23:22:04+09:00 Subject: Class instance variables, class variables and constants Hello, Thanks to the code in attach, I've figured out what is now completely obvious to me: CLASS VARIABLE (@@something) - Inherited by subclasses - SAME ALLOCATION: If it's changed in Child, then it will change in Parent CONSTANTS (Something) - Inherited by subclasses - PRIVATE ALLOCATION: If it's changed in Child, it will NOT change in parent CLASS INSTANCE VARIABLES (@something) - NOT Inherited by subclasses. They will simply be nil. Scope = Class - DIFFERENT ALLOCATION: If it's changed in Child, it will NOT change in parent I am sending this email to the mailing list so that if some Ruby newbie has the same problem with variable scope, they might find this email (and find it useful). David A. Black wrote: "I think Matz is planning to change class variables in 2.0 so that they are class/module scoped rather than hierarchy scoped." But wouldn't it make class variables (@@something) the same as class instance variables (@something)? Here's the code I used to figure things out: ----------------------------------------------------- #!/usr/local/bin/ruby -w class Outer @@class_var=10 GlobalVar=100 @civ=1000 class Second < Outer @@class_var=11 GlobalVar=101 @civ=1001 def Second::class_var @@class_var end def Second::class_var=(what) @@class_var=what end def Second::civ @civ end def Second::civ=(what) @civ=what end end def Outer::class_var return @@class_var end def Outer::class_var=(what) @@class_var=what end def Outer::civ @civ end def Outer::civ=(what) @civ=what end end class Third < Outer def self::class_var return @@class_var end def Third::class_var=(what) @@class_var=what end def Third::class_var @@class_var end def Third::class_var=(what) @@class_var=what end def Third::civ @civ end def Third::civ=(what) @civ=what end end puts "Outer" p Outer::class_var p Outer::GlobalVar p Outer::civ puts "Outer::Second" p Outer::Second::class_var p Outer::Second::GlobalVar p Outer::Second::civ puts "Third" p Third::class_var p Third::GlobalVar p Third::civ puts puts Outer::Second::class_var=13 Outer::Second::GlobalVar=103 Outer::Second::civ=1003 puts "Outer" p Outer::class_var p Outer::GlobalVar p Outer::civ puts "Outer::Second" p Outer::Second::class_var p Outer::Second::GlobalVar p Outer::Second::civ puts "Third" p Third::class_var p Third::GlobalVar p Third::civ puts puts Third::class_var=14 Third::GlobalVar=104 Third::civ=1004 puts "Outer" p Outer::class_var p Outer::GlobalVar p Outer::civ puts "Outer::Second" p Outer::Second::class_var p Outer::Second::GlobalVar p Outer::Second::civ puts "Third" p Third::class_var p Third::GlobalVar p Third::civ puts puts # Just checking... p Third::Second::class_var p Third::Second::GlobalVar p Third::Second::civ