From: Trans Date: 2007-09-01T00:18:36+09:00 Subject: Re: special super call for module class-level inclusion On Aug 29, 4:58 pm, Daniel DeLorme wrote: > The problem piqued my interest so despite my best attempts at > self-control I wound up writing code for it :-/ > > class Module > attr_accessor :class_methods_module > def class_methods(&block) > self.class_methods_module ||= begin > mod = Module.new > core = (class << self; self; end) > prev = core.method(:included) > core.send(:define_method, :included) do |into_class| > prev.call(into_class) > into_class.extend(mod) > end > mod > end > class_methods_module.class_eval(&block) > end > end > > class Class > def class_methods(&block) > instance_eval(&block) > end > end Nice work! That's not the easiest piece of meta-code to write. But has been touched on before. Facets has had a version of this for a while now, used extensively by Nitro/Og, written primarily by Daniel Schierbeck. It looks like this: class Module alias_method :append_features_without_class_extension, :append_features def class_extension(&block) @class_extension ||= Module.new do def self.append_features(mod) append_features_without_class_extension(mod) end end @class_extension.module_eval(&block) if block_given? @class_extension end private :class_extension def append_features(mod) append_features_without_class_extension(mod) mod.extend(class_extension) if mod.instance_of? Module mod.__send__(:class_extension).__send__(:include, class_extension) end end end class Class undef_method :class_extension end Your definition of Class's method is an interesting idea and may make a good adjustment Facets' undef_method. On the downside, this approach doesn't RDoc well --actually it doesn't RDOc at all. Also, many people seem put off by the general aesthetic of it too. As of late I've been leaning toward the idea of the old ClassMethods module solution, but modifying #include (or a similar new method) to handle the extension rather than using the included callback. (Personally, I'd also prefer a better name than ClassMethods, but that's of minor consequence). T.