From: "trans. (T. Onoma)" Date: 2004-11-15T06:37:11+09:00 Subject: Re: Dynamic define_method on class creation per module namespace On Sunday 14 November 2004 04:08 pm, itsme213 wrote: | 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 Thanks, I may be able to work with that. If I can just get all_modules_that_have_included_ClassMethods :) | 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. I am doing something very similar with a markup parser (made-up example): tr = token_registry { block :literal, '"""', '"""' line :strong, '*', '*' line :italic, '_', '_' } One of things that made the methods even better was not having to worry about the namespace for the classes themselves --in other words, the classes Block and Line themselves are, in a way, private. T.