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 > Redefine a class with same name
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Redefine a class with same name

Réponse
 
LinkBack Outils de la discussion
Vieux 09/03/2008, 12h01   #1
Martin Boese
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Redefine a class with same name


Is there any way to to remove the definition of a class so I can reuse its
name? Like below I'd like to reuse the name 'A' but since it's already
defined with a different superclass I get a 'TypeError: superclass mismatch
for class A'.

------------------------------------------------------

class X
end
class Y
end

class A < X
end

==> Here I'd like to remove A's definition so I can:

class A < Y
end

==> without getting a: 'TypeError: superclass mismatch for class A'

------------------------------------------------------


Thanks,
Martin


  Réponse avec citation
Vieux 09/03/2008, 13h50   #2
Rick DeNatale
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Redefine a class with same name

On 3/9/08, ts <decoux@moulon.inra.fr> wrote:
> >>>>> "M" == Martin Boese <boesemar@gmx.de> writes:

>
> M> Is there any way to to remove the definition of a class so I can reuse its
> M> name? Like below I'd like to reuse the name 'A' but since it's already
> M> defined with a different superclass I get a 'TypeError: superclass mismatch
> M> for class A'.
>
> Just remove the constant
>
> M> class A < X
> M> end
>
> Object.send(:remove_const, :A)
>
> M> class A < Y
> M> end


Keep in mind though, that this only removes the binding between the
constant and the class, it doesn't actually allow you to change the
old class, and any existing instances will still be instances of the
old class not the new one:

k$ irb
irb(main):001:0> class A
irb(main):002:1> def foo
irb(main):003:2> end
irb(main):004:1> end
=> nil
irb(main):005:0> a = A.new
=> #<A:0x6e578>
irb(main):006:0> a.foo
=> nil
irb(main):007:0> Object.send(:remove_const, :A)
=> A
irb(main):008:0> a.foo
=> nil
irb(main):009:0> a.class
=> A
irb(main):010:0> A
NameError: uninitialized constant A
from (irb):10
irb(main):011:0> class A
irb(main):012:1> def bar
irb(main):013:2> end
irb(main):014:1> end
=> nil
irb(main):015:0> a.bar
NoMethodError: undefined method `bar' for #<A:0x6e578>
from (irb):15
irb(main):016:0>


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

  Réponse avec citation
Vieux 09/03/2008, 16h15   #3
Martin Boese
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Redefine a class with same name

Excellent! That's fine for me. I only have the case for a web framework of
mine when using with fastcgi as different calls might use same class names
which I can unregister now.

Thanks,
martin


On Sunday 09 March 2008 13:50:59 Rick DeNatale wrote:
> On 3/9/08, ts <decoux@moulon.inra.fr> wrote:
> > >>>>> "M" == Martin Boese <boesemar@gmx.de> writes:

> >
> > M> Is there any way to to remove the definition of a class so I can
> > reuse its M> name? Like below I'd like to reuse the name 'A' but since
> > it's already M> defined with a different superclass I get a 'TypeError:
> > superclass mismatch M> for class A'.
> >
> > Just remove the constant
> >
> > M> class A < X
> > M> end
> >
> > Object.send(:remove_const, :A)
> >
> > M> class A < Y
> > M> end

>
> Keep in mind though, that this only removes the binding between the
> constant and the class, it doesn't actually allow you to change the
> old class, and any existing instances will still be instances of the
> old class not the new one:
>
> k$ irb
> irb(main):001:0> class A
> irb(main):002:1> def foo
> irb(main):003:2> end
> irb(main):004:1> end
> => nil
> irb(main):005:0> a = A.new
> => #<A:0x6e578>
> irb(main):006:0> a.foo
> => nil
> irb(main):007:0> Object.send(:remove_const, :A)
> => A
> irb(main):008:0> a.foo
> => nil
> irb(main):009:0> a.class
> => A
> irb(main):010:0> A
> NameError: uninitialized constant A
> from (irb):10
> irb(main):011:0> class A
> irb(main):012:1> def bar
> irb(main):013:2> end
> irb(main):014:1> end
> => nil
> irb(main):015:0> a.bar
> NoMethodError: undefined method `bar' for #<A:0x6e578>
> from (irb):15
> irb(main):016:0>




  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 05h17.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09665 seconds with 11 queries