title: Configure a class with a block
body: <p>I have a base module that has some logic in it and it gets included inside various classes. Now I need a <code>configure</code> block that sets some configuration how the class should behave.</p>
  
  <p>I tried the following code:</p>
  
  <pre><code>class MyConfig
    attr_accessor :foo, :bar
  end
  
  module BaseModule
    def self.included base
      base.include InstanceMethods
      base.extend ClassMethods
    end
  
    module ClassMethods
      attr_reader :config
  
      def configure &amp;block
        @config = MyConfig.new
        block.call(@config)
      end
    end
  
    module InstanceMethods
      def do_something
        puts self.class.config.inspect
      end
    end
  end
  
  class MyClass1
    include BaseModule
  
    configure do |config|
      config.foo = "foo"
    end
  end
  
  class MyClass2
    include BaseModule
  
    configure do |config|
      config.bar = "bar"
    end
  end
  
  MyClass1.new.do_something
  #&lt;MyConfig:0x007fa052877ea0 @foo="foo"&gt;
  MyClass2.new.do_something
  #&lt;MyConfig:0x007fa052877ce8 @bar="bar"&gt;
  </code></pre>
  
  <blockquote>
    <ol>
    <li>I'm unsure if an instance variable <code>@config</code> for a module / class is the best way to configure a class. Is this the right way, or are there any better solutions?</li>
    <li>Is it good to also use <code>base.extend ClassMethods</code> when the module only gets included with <code>include BaseModule</code>? A developer could expect that only instance methods get included, but as a side effect there are also class methods extended.</li>
    </ol>
  </blockquote>
  
  <p><strong>Update</strong></p>
  
  <p>I've added a <code>MyConfig</code> class, from which a new instance is created. This has the benefit that you can use <code>config.foo = "foo"</code>.</p>