title: How do I override a variable in a Ruby subclass without affecting the superclass?
body: <p>Let's say I have a class with a few "static" variables. I want to a subclass of that class to be able to override those variables without affecting the original class. This isn't possible using class variables, since those appears to be shared between subclasses and superclasses:</p>
  
  <pre><code>class Foo
    @@test = "a"
  
    def speak; puts @@test; end
  end
  
  class Bar &lt; Foo
    @@test = "b"
  end
  
  Bar.new.speak
  # b
  
  Foo.new.speak
  # b
  </code></pre>
  
  <p>It isn't possible using constants either:</p>
  
  <pre><code>class Foo
    TEST = "a"
  
    def speak; puts TEST; end
  end
  
  class Bar &lt; Foo
    TEST = "b"
  end
  
  Bar.new.speak
  # a
  
  Foo.new.speak
  # a
  </code></pre>
  
  <p>Methods defined in the superclass ignores constants in the subclass.</p>
  
  <p>The obvious workaround is to define methods for the variables that needs to be "overridable":</p>
  
  <pre><code>class Foo
    def test; "a"; end
  end
  </code></pre>
  
  <p>But that feels like a hack. I feel like this should be possible using class variables and that I'm probably just doing it wrong. For example, when I subclass <code>Object</code> (which is what happens by default):</p>
  
  <pre><code>class Foo &lt; Object
    @@bar = 123
  end
  
  Object.class_variable_get(:@@bar)
  # NameError: uninitialized class variable @@bar in Object
  </code></pre>
  
  <p>Why isn't <code>@@bar</code> set on <code>Object</code> like it was in my <code>Bar &lt; Foo</code> example above?</p>
  
  <hr>
  
  <p>To summarize: how do I override a variable in a subclass without affecting the superclass?</p>