The following code sets a class variable in the class C from a class method, and shows that it is accessible from an instance method:
class C
def self.set_a
@@a = 1
end
def get_a
@@a
end
end
C.set_a
C.new.get_a #=> 1
If I replace @@a in the class method set_a with @a, so that it creates a class instance variable instead of a class variable, can I still access it from within the instance method get_a?
def get_a; @a; end, asc = C.new; c.get_awould return the value ofc's instance_variable@a. You therefore will need to employ two methods withinget_ato obtain the value of the class instance variable@a. The first to obtainc's class; the second, invoked onc's class, to obtain the value of the variable.CandC.neware two completely different objects that have absolutely northing to do with each other. The whole point of instance variables is that one object cannot access another object's instance variables, unless that object explicitly exposes them in some manner.