title: How to override class constants and methods from a module?
body: <p>I am trying to create a module to define some methods and constants for some classes.</p>
  <p>This means the same method or constant can be defined in both the module and the class that includes the module.</p>
  <p>In that scenario, I want to have control (preferably in the module definition, or maybe from the caller, without changing the class code) over which one takes precedence.</p>
  <p>Often, I want the method or constant defined in the module to be used.</p>
  <p>However, it looks like those defined in the class always get used unless I delete them from the class.</p>
  <pre><code>module A
    def self.included(clazz)
      const_set(:CONST, clazz.name == &quot;B&quot; ? &quot;constant from module A for B&quot; : &quot;constant from module A for C&quot;)
  
      define_method(:a) do
        &quot;from module A&quot;
      end
    end
  end
  
  class B
    include A
    CONST = &quot;constant from class B&quot;
    def a
      &quot;from class B&quot;
    end
  end
  
  class C &lt; B
    include A
    CONST = &quot;constant from class C&quot;
    def a
      &quot;from class C&quot;
    end
  end
  
  b = B.new
  c = C.new
  
  p b.a, c.a
  p B::CONST, C::CONST
  </code></pre>
  <p>Running this on <a href="https://replit.com/@EricNa1/FriendlyFumblingOctagons#main.rb" rel="nofollow noreferrer">this REPL</a> prints</p>
  <pre><code>main.rb:3: warning: already initialized constant A::CONST
  main.rb:3: warning: previous definition of CONST was here
  &quot;from class B&quot;
  &quot;from class C&quot;
  &quot;constant from class B&quot;
  &quot;constant from class C&quot;
  </code></pre>
  <p>Is there a way I can make this output</p>
  <pre><code>&quot;from class A&quot;
  &quot;from class A&quot;
  &quot;constant from module A for B&quot;
  &quot;constant from module A for C&quot;
  </code></pre>
  <p>without deleting constant <code>CONST</code> or method <code>a</code> from class B or C?</p>