title: Can you add a Class Method to the Core Module Class in Ruby?
body: <p>Is it possible to add a core method to the Module class in Ruby?  I want to do something like this (messed around with the idea yesterday, just whipped this together in a second):</p>
  
  <pre><code>module MyModule
    base do
      has_many :something
    end
  end
  
  # implementation, doesn't work though... reason for the question
  Module.class_eval do
    def self.base(&amp;block)
      class_eval do
        def self.included(base)
          base.class_eval(&amp;block)
        end
      end
    end
  end
  </code></pre>
  
  <p>If I create a module, I can't access that method:</p>
  
  <pre><code>$ module User; end
  $ User.base
  NoMethodError: undefined method `base' for User:Module
  </code></pre>
  
  <p>Any ways to do this?</p>
  
  <p><strong>Update</strong></p>
  
  <p>This works!  Thanks @Jörg W Mittag.  Makes it much easier to read for Rails <code>has_many</code> and such:</p>
  
  <pre><code>class Module
    def base(&amp;block)
      metaclass = class &lt;&lt; self; self; end
      # add the method using define_method instead of def x.say so we can use a closure
      metaclass.send :define_method, :included do |base|
        base.class_eval(&amp;block) unless block.nil?
        base.class_eval &lt;&lt;-EOF
          extend  #{name}::ClassMethods
          include #{name}::InstanceMethods
        EOF
      end
      block
    end
  end
  </code></pre>
  
  <p>Like this example:</p>
  
  <pre><code>module UserRoleBehavior
    base do
      has_many :user_roles
      has_many :roles, :through =&gt; :user_roles
    end
  
    module ClassMethods
      # ...
    end
  
    module InstanceMethods
      # ...
    end
  end
  
  class User &lt; ActiveRecord::Base
    include UserRoleBehavior
  end
  </code></pre>
  
  <p>Cheers!</p>