# Forcing superclass initialization

**URL:** <https://www.ruby-forum.com/t/forcing-superclass-initialization/62260>\
**Category:** Ruby\
**Created:** [June 1, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/forcing-superclass-initialization/62260 "2006-06-01T23:39:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [June 1, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/forcing-superclass-initialization/62260/1 "2006-06-01T23:39:21Z")

</div>

So if the a negligent subclass refuses to call its superclass initialize  
method, instance variables in the superclass may not get initialized.  
Not a good thing. Is there some way to force the superclass method to  
get called?

---

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [June 2, 2006, 2:31am UTC](https://www.ruby-forum.com/t/forcing-superclass-initialization/62260/2 "2006-06-02T02:31:56Z")

</div>

Actually I find that an interesting question. I’ve sometimes wondered  
about “forced super” for #initialize, as well and all methods actually.  
The problem though is at least two fold.

1. How do you “override”. What if the behavior needs to be completely  
different and you want none of what comes before?

2. A forced super has to be called at some specific point. Should it be  
before eveything else, or after? What if you want things to occur  
before and after?

In the past I’ve suggested that a better appraoch would be either AOP  
on the ::new call --if we ever get a bonafide AOP system. Or at least a  
“preinitialize” callback in which there is a “forced” execution  
throughout the class hierarchy.

T.

---

<div class="post-metadata">

**Author:** ![ara-vartanian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@ara-vartanian](https://www.ruby-forum.com/u/ara-vartanian)\
**Post date:** [June 2, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/forcing-superclass-initialization/62260/3 "2006-06-02T16:59:04Z")

</div>

yes, those are good points. Ideal would be callbacks right after the  
object was created or right before. And then there’s the issue of the  
order of initialization within the inheritance hierarchy. I was  
wondering if there was a primitive solution in implementing a callback  
for whenever a new instance of any object is created, checking to see  
its type, and performing initialization if it is one of those classes  
which has registered for mandatory initialization. But I do not know  
where in the Ruby APIsuch a callback exists.

is there some philosophical reason the Ruby community is against this?
