# Self.inherited but wait until subclass is fully defined?

**URL:** <https://www.ruby-forum.com/t/self-inherited-but-wait-until-subclass-is-fully-defined/163466>\
**Category:** Ruby\
**Created:** [March 20, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/self-inherited-but-wait-until-subclass-is-fully-defined/163466 "2009-03-20T17:26:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [March 20, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/self-inherited-but-wait-until-subclass-is-fully-defined/163466/1 "2009-03-20T17:26:02Z")

</div>

Howdy. I am a class. There are certain rules for what you are supposed  
to do when you subclass me, so as a sanity check I wish to introspect  
your subclass to make sure it’s obeying the contract.

However, my problem is that self.inherited is called too early.

class C # this is me  
def self.inherited§  
#whatever  
super  
end  
end

class B \< C # this is you

# C’s self.inherited is called right here

def yourFirstMethod

# etc.

end  
end

So I can’t, in my self.inherited, ask about what methods you have  
defined, because you haven’t defined them yet. This must have come up  
before; is there an easy solution? Thx - m.

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [March 20, 2009, 5:40pm UTC](https://www.ruby-forum.com/t/self-inherited-but-wait-until-subclass-is-fully-defined/163466/2 "2009-03-20T17:40:42Z")

</div>

On 20.03.2009 17:22, matt neuburg wrote:

> end  
> defined, because you haven’t defined them yet. This must have come up  
> before; is there an easy solution? Thx - m.

Even after the class definition methods can be added. You could catch  
that with method\_added but if you require a minimum set of methods to be  
defined before class is used (e.g. instantiated) this will be difficult  
to track. One option would be to define method initialize in your class  
and do the check there. But this is still fragile because it might not  
be guaranteed that sub classes invoke it from their initialize…

Another option would be to override the sub class’s method “new” so you  
can do the check there. This might be a bit more robust but even “new”  
can be overridden by subclasses (although it isn’t generally).

I’d probably just omit the check. If any of your superclass methods  
invokes a method that is not defined you’ll see this at runtime anyway.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [March 20, 2009, 7:38pm UTC](https://www.ruby-forum.com/t/self-inherited-but-wait-until-subclass-is-fully-defined/163466/3 "2009-03-20T19:38:38Z")

</div>

On Mar 20, 2009, at 09:37 , Robert K. wrote:

> > end
> 
> even “new” can be overridden by subclasses (although it isn’t  
> generally).
> 
> I’d probably just omit the check. If any of your superclass methods  
> invokes a method that is not defined you’ll see this at runtime  
> anyway.

I agree with this in general for Matt’s particular problem. The  
smalltalk pattern of defining all contracted methods and having them  
raise SubclassResponsibility is a good one to use in this situation.

BUT… There have been many times where I’ve wanted a “class closed”  
hook to go along with inherited (essentially a “class opened” hook).  
Having both makes it very easy for me to do lots of things with my  
language tools. I can instrument added methods quite easily with that  
mechanism. It’d be nice to have this extra hook.

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [August 26, 2009, 9:27am UTC](https://www.ruby-forum.com/t/self-inherited-but-wait-until-subclass-is-fully-defined/163466/4 "2009-08-26T09:27:44Z")

</div>

matt neuburg wrote:

> Howdy. I am a class. There are certain rules for what you are supposed  
> to do when you subclass me, so as a sanity check I wish to introspect  
> your subclass to make sure it’s obeying the contract.

Is this even a good idea?

What if I wanted to create an abstract subclass that doesn’t obey the  
contract – which is OK because it won’t be instantiated – but whose  
own subclasses do?

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [March 20, 2009, 7:55pm UTC](https://www.ruby-forum.com/t/self-inherited-but-wait-until-subclass-is-fully-defined/163466/5 "2009-03-20T19:55:36Z")

</div>

2009/3/20 Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > def self.inherited(p)  
> > > So I can’t, in my self.inherited, ask about what methods you have
> 
> SubclassResponsibility is a good one to use in this situation.  
> So, maybe a little helper:

class Module  
def abstract\_methods(\*names)  
names.each do |name|  
define\_method(name) { raise SubclassResponsibility }  
end  
end  
end

For example

class MyClass  
abstract\_methods :foo, :bar  
end

class Whizz \< MyClass  
def foo; “foo!”; end  
end

w = Whizz.new  
w.foo #=\> “foo!”  
w.bar #=\> error: SubclassResponsibility

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [August 26, 2009, 6:14pm UTC](https://www.ruby-forum.com/t/self-inherited-but-wait-until-subclass-is-fully-defined/163466/6 "2009-08-26T18:14:58Z")

</div>

On Fri, Mar 20, 2009 at 8:35 PM, Ryan D.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> BUT… There have been many times where I’ve wanted a “class closed” hook to  
> go along with inherited (essentially a “class opened” hook). Having both  
> makes it very easy for me to do lots of things with my language tools. I can  
> instrument added methods quite easily with that mechanism. It’d be nice to  
> have this extra hook.  
> I am not sure I understand. Do you mean “class frozen hook”? I do not  
> really know when a “class closed” hook should be triggered.

Robert

---

<div class="post-metadata">

**Author:** ![matt\_neuburg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4af34b/32.png) [@matt\_neuburg](https://www.ruby-forum.com/u/matt_neuburg)\
**Post date:** [March 20, 2009, 8:23pm UTC](https://www.ruby-forum.com/t/self-inherited-but-wait-until-subclass-is-fully-defined/163466/7 "2009-03-20T20:23:44Z")

</div>

Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Another option would be to override the sub class’s method “new” so you  
> can do the check there. This might be a bit more robust but even “new”  
> can be overridden by subclasses (although it isn’t generally).

That’s a good idea, I’ll look into it - thanks! m.
