title: Module and class variable scope in ruby
body: <p>I am still trying to clearly understand Module/Class/Instance variables ...</p>
  
  <p>My code currently looks something like this ...</p>
  
  <pre><code>module Foo
    @@var1 ={}
    @@var2 =[]
    @@var3 = nil
  
    def m1(value)
      @@var2 &lt;&lt; value
    end
  
    def m2(value)
      @@var1[@@var3]=value
    end
  end
  
  class Bar
    include Foo
    p @@var1
  end
  
  class Bar2
    include Foo
    p @var1
  end
  </code></pre>
  
  <p>I am trying to create a module that contains a class-wide configuration for how each class will behave. The configuration is stored in @@var1 and @@var2. Using this code the variables are shared across ALL classes that include the module. This is not the desire result, I want each class to have it's own behavior configuration.</p>
  
  <p>I have also tried creating a single class that includes the module and also creates the variables but then the variables are not accessible by the module.</p>
  
  <pre><code>module Foo
  
    def m1(value)
      @@var2 &lt;&lt; value
    end
  
    def m2(value)
      @@var1[@@var3]=value
    end
  end
  
  class T
    @@var1 ={}
    @@var2 =[]
    @@var3 = nil
  
    include foo
  end
  
  class Bar &lt; T
    p @@var1
  end
  
  class Bar2 &lt; T
    p @var1
  end
  </code></pre>
  
  <p>I have also read that having modules with class variables is not good coding style but I cannot think of a way to achieve my functionality with this ... </p>
  
  <p>Thanks in advance for any help</p>