On Nov 8, 1:44 pm, Greg Weeks <gregwe... / shaw.ca> wrote:
> I'm still curious about the most common practice, if there is one,
> specifically about creating little "mini-languages" that can be used
> inside class definitions.  (Yes, I'm wondering about Rails.)  My example
> with explicit class methods now smells pretty bad to me, for example.
> Could modules (using "define_method" and other magic) "extended" into
> class objects be considered the norm?  This is a more vague question,
> but I'm putting it out FWIW.

It's what Rails and its component libraries mostly do. For example:

module ActiveRecord
  module Associations
    def self.included(base)
      base.extend(ClassMethods)
    end
    ...
    module ClassMethods
      def has_many(association_id, options = {}, &extension)
        ...
      end
    end
  end
end

ActiveRecord::Base.class_eval do
  ...
  include ActiveRecord::Associations
  ...
end


And that's how you get the has_many method:
  class Person < ActiveRecord::Base
    has_many :cats
  end