"trans. (T. Onoma)" <transami / runbox.com> wrote
>   module M
>
>     include ClassMethods
>
>     class A
>       def initialize ; puts "A" ; end
>     end
>
>     class B
>       def initialize ; puts "B" ; end
>     end
>
>     # etc
>
>   end

I think you can hook into Class#inherited at the Object level, thusly:

class Object
    def self.inherited(child)
        for m in all_modules_that_have_included_ClassMethods
            if m.const_defined(child.name) && m.const_get(child.name)==child
                generated = "
                    def #{child.name.downcase}(*args, &b)
                        #{child.name).new(*args, &b)
                    end"
                m.module_eval generated
            end
        end
    end
end

I'm curious what you are trying to do with this. I am doing something
similar to allow easy creation of tree structures, but I need instance (not
module) methods to grab hold of the constructed objects, plus some 'typed'
attribute macros to know what to instantiate.

car 'A' {
    engine '8-cylinder' {
        valve { ...}
        valve { ...}
        valve { ...}
    }
}

I plan to allow some conveinent cross-tree references as well.