@my_var , in your sample code, is an instance variable of the MainController class. That is, it is an instance variable of a class, not an instance variable. It exists in a completely different scope to the instance variable associated with the class instance.
Inside the body of your instance methods index and index2 you are trying to dereference the instance variable for an object that is an instance of the MainController class, but you did not specify this instance variable anywhere, so you will return nil .
If you want to use @my_var as a class instance variable, you can get its value from the class instance as follows:
var1 = self.class.instance_variable_get(:@my_var)
Class variables are indicated by the @@ prefix, and their use is not fully encouraged. After a couple of minutes, Google will tell you why.
D_Bye
source share