If I have a module that includes two variables like this:
module Test_module @my_var1 = "init string1" @@my_var2 = "init string 2"then I include that module in a class:
class Test_class1 include Test_moduleand create two instances of that class:
myobj1 = Test_class1.newmyobj2 = Test_class1.newmy_var1 and my_var2 will now exist as attributes of myobj1 and myobj2. However:
If I run
myobj1.my_var1 = "in obj1"myobj2.my_var1 = "in obj2"Then each object will have a distinct vale in their my_var1 variable. However if I run:
myobj1.my_var2 = "new my_var2 value"Then this changes the "class" (module) variable's value. As there is only ever one module, this means myobj2.my_var2 points to the same memory location and hence myobj2.my_var2 == "new my_var2 value"
What makes this a little more interesting, if I include the module in another class
class Test_class2 include Test_moduleand create an instance of that class:
myobj3 = Test_class2.newmyobj3.my_var2 ALSO points to the same memory location and hence myobj3.my_var2 == myobj2.my_var2 == myobj2.my_var1
So the @@ variables in a module are specific to the module, regardless of what classes it is included in.
Last but not least, any references to @my_var1 within the Module (other than the initialisation string!) impact the instance variable in the class that included the Module.