PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Race condition in class inheritance
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Race condition in class inheritance

Réponse
 
LinkBack Outils de la discussion
Vieux 14/09/2007, 15h08   #1 (permalink)
Gaspard Bucher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Race condition in class inheritance

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

  Réponse avec citation
Vieux 14/09/2007, 16h52   #2 (permalink)
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Race condition in class inheritance

Gaspard Bucher 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

> @@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



--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Vieux 14/09/2007, 18h54   #3 (permalink)
Gaspard Bucher
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Race condition in class inheritance

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

Gaspard

2007/9/14, Joel VanderWerf <vjoel@path.berkeley.edu>:
> Gaspard Bucher 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
>
> > @@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

>
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
>
>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h37.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09409 seconds with 11 queries