Err... the basic idea behind class variables is that they CANNOT be accessed from outside the class or its descendant (you are sure you are speaking of a class variable, one beginning with @@ ?).
So, the only way I see to usre a Class_A class variable from Class_B is to make Class_B a descendant of Class_B. Or to make Class_A and Class_B descendant of the same Class_Parent.
--------------------------------------------------------------------------------------
1st solution if Class_A and Class_B share more the class variables
class Class_A
def initialize
@@z = 0
end
def z=( val)
@@z = val
end
end
class Class_B < Classe_A
end
--------------------------------------------------------------------------------------
2nd solution if Class_A and Class_B have just have the class variable to share
class Class_Parent
def initialize
@@z = 0
end
def z=( val)
@@z = val
end
end
class Class_A < Classe_Parent
end
class Class_B < Classe_Parent
end
--------------------------------------------------------------------------------------
and a
3td solution that came up while I was writing the previous ones
Just create an instance of a sharing 3rd class in both Class_A and Class_B (more practical if Class_A and Class_B have different parent classes)
class Class_Shared
def initialize
@@z = 0
end
def z=(val)
@@z = val
end
end
class Class_A
attr_accessor :shared
def initialize
@shared = Class_Shared.new
...
end
end
class Class_B
attr_accessor :shared
def initialize
@shared = Class_Shared.new
...
end
end
you can even have
class Class_A < Class_A_Parent
class Class_B < Class_B_Parent
when instanciating Class_A and Class_B like this
a = Class_A.new
b = Class_B.new
you get the shared class variable via
Hope I made no mistake....