Wait let's stop for a second so we can get off on the right foot. Variables preceded by a
single "@" are known as instance variables. Variables that are prefixed with
two "@". Like this, @@test = 0, are known as class variables. Now let's say you have an instance variable in one class and would like to manipulate it from inside another class. There are two ways that I can think of off the top of my head. The second class has an instance of the first class within it at all times, through that instance you can manipulate the attribute you want from the first class. Or the second class (inherits/derives/is the child of) the first class.
Here's what I mean by having an instance of the first class within the second class.
class Test_A
attr_accessor :a
def initialize
@a = 2
end
end
class Test_B
attr_accessor :test
def initialize
@test = Test_A.new
end
end
test_instance = Test_B.new
test_instance.test.a = 5
Now let's see it using inheritance.
class Test_A
attr_accessor :a
def initialize
@a = 2
end
end
class Test_B < Test_A
def initialize
super # Call Test_A's initialize
end
end
test_instance = Test_B.new
p test_instance.a # Prints "2"
test_instance.a = 5
Good luck with it Khoa! :thumb: