From: Brian Candler Date: 2008-10-29T21:26:14+09:00 Subject: Re: inherited behaviour. Macario Ortega wrote: > Hi, I have this situation where it would save me some typing if the > inherited callback was called after the class was fully defined (with > all its methods). But it's never "fully defined", as it can be extended at any time in the future. (Or depending on how you look at it, it's "fully defined" at the instant the subclass is created, before any methods are added to it) > is there a way I can get "yeah!" instead of "damn!"? class Class alias :old_new :new def new(parent = Object, &blk) child = old_new(parent, &blk) parent.delayed_inherited(child) if parent.respond_to?(:delayed_inherited) child end end class A def self.delayed_inherited(klass) p klass.respond_to?(:my_method) ? "yeah!" : "damn!" end end B = Class.new(A) do def self.my_method end end p "too late!" if B.respond_to?(:my_method) ### Or you could just trigger it explicitly: class Class def ok return unless superclass.respond_to?(:delayed_inherited) superclass.delayed_inherited(self) end end class A def self.delayed_inherited(klass) p klass.respond_to?(:my_method) ? "yeah!" : "damn!" end end class B < A def self.my_method end ok end p "too late!" if B.respond_to?(:my_method) ### That's only two characters to type for each class definition :-) -- Posted via http://www.ruby-forum.com/.