# Race condition in class inheritance

**URL:** <https://www.ruby-forum.com/t/race-condition-in-class-inheritance/114769>\
**Category:** Ruby\
**Created:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/race-condition-in-class-inheritance/114769 "2007-09-17T13:25:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/race-condition-in-class-inheritance/114769/1 "2007-09-17T13:25:34Z")

</div>

I need to identify classes from a string called kpath (class path)  
reflecting the class inheritance. The string is made of the first  
letter of each class. This is what I have now:  
class Node  
@@classes = {‘N’ =\> self}  
def self.inherited(child)  
super  
@@classes[child.kpath] = child  
end

def self.kpath  
self == Node ? ksel : (superclass.kpath + ksel)  
end

def self.ksel  
self.to\_s[0…0]  
end

def self.class\_from\_kpath(kpath)  
@@classes[kpath]  
end  
end

class Page \< Node  
end

class Document \< Page  
end

class Draft \< Page  
def self.ksel  
‘A’  
end  
end

puts Node.class\_from\_kpath(‘N’) # got ‘Node’, ok.  
puts Node.class\_from\_kpath(‘NPD’) # wanted ‘Document’, got ‘Draft’  
puts Node.class\_from\_kpath(‘NPA’) # wanted ‘Draft’, got nil

I understand that Draft’s ksel method is not known at the time  
Node.inherited is called.

How can I have Node.inherited being called once the complete child  
class is built ?

Thanks for your answers.  
Gaspard

---

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/race-condition-in-class-inheritance/114769/2 "2007-09-25T23:03:53Z")

</div>

Gaspard B. wrote:

> I need to identify classes from a string called kpath (class path)  
> reflecting the class inheritance. The string is made of the first  
> letter of each class. This is what I have now:

Defer building the hash until class\_from\_path is called.

One quick and dirty set of changes that does it:

> class Node  
> @@classes = {‘N’ =\> self}

Replace:

> def self.inherited(child)  
> super  
> @@classes[child.kpath] = child  
> end

With:

@@unhandled\_children = []  
def self.inherited(child)  
super  
@@unhandled\_children \<\< child  
end

> def self.kpath  
> self == Node ? ksel : (superclass.kpath + ksel)  
> end
> 
> def self.ksel  
> self.to\_s[0…0]  
> end
> 
> def self.class\_from\_kpath(kpath)

Add:

```
 while child = @@unhandled_children.pop
   @@classes[child.kpath] = child
 end
```

---

<div class="post-metadata">

**Author:** ![gazoduc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c5a1d2/32.png) [@gazoduc](https://www.ruby-forum.com/u/gazoduc)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/race-condition-in-class-inheritance/114769/3 "2007-09-25T23:04:31Z")

</div>

Thanks for the idea, that’s a good workaround.

Gaspard

2007/9/14, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
