# Inherited called at different times

**URL:** <https://www.ruby-forum.com/t/inherited-called-at-different-times/81032>\
**Category:** Ruby\
**Created:** [January 3, 2007, 7:31am UTC](https://www.ruby-forum.com/t/inherited-called-at-different-times/81032 "2007-01-03T07:31:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![darwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@darwin](https://www.ruby-forum.com/u/darwin)\
**Post date:** [January 3, 2007, 7:31am UTC](https://www.ruby-forum.com/t/inherited-called-at-different-times/81032/1 "2007-01-03T07:31:13Z")

</div>

Hi.

I have encountered a strange phenomenon about when self.inherited is  
called. Take the following code:

class Base  
def self.inherited(sub)  
p “Base is being inherited”  
end  
end

class A \< Base  
p “Declaring A”  
end

B = Class.new(Base) do  
p “Declaring B”  
end

To my surprise, this prints (in ruby 1.8.4)  
“Base is being inherited”  
“Declaring A”  
“Declaring B”  
“Base is being inherited”

I was expecting that “Base is…” and “Declaring…” would alternate  
nicely.

Although to me that looks like a bug, I presume it isn’t, but I would  
greatly appreciate a justification for this behaviour. Note that the  
documentation on inherited is not clear on when it is supposed to be  
called.

If someone wonders why I care… well a rails plugin I use calls  
read\_inheritable\_attribute and write\_inheritable\_attribute. These are  
defined in an extension to Class that uses inherited and because of the  
calling sequence, it works only for the usual declaration but not for  
the anonymous one.

Thanks,

Marc-AndrÃ© Lafortune  
[http://www.marc-andre.ca](http://www.marc-andre.ca)
