From f60b732940e4785c522398dff2a409ac616351ec Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 31 May 2009 23:07:31 +0200 Subject: AS guide: explains class removal --- .../guides/source/active_support_overview.textile | 44 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'railties/guides/source') diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 219c3c5b56..0d9753cb84 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -108,7 +108,7 @@ If for whatever reason an application loads the definition of a mailer class and h4. Subclasses -The method +Class#subclasses+ returns the names of all subclasses of a given class as an array of strings. That comprises not only direct subclasses, but all descendants down the hierarchy: +The +subclasses+ method returns the names of all subclasses of a given class as an array of strings. That comprises not only direct subclasses, but all descendants down the hierarchy: class C; end @@ -133,6 +133,48 @@ The order in which these class names are returned is unspecified. See also +Object#subclasses_of+ in "Extensions to All Objects FIX THIS LINK":FIXME. +h4. Class Removal + +Roughly speaking, the +remove_class+ method removes the class objects passed as arguments: + + +Class.remove_class(Hash, Dir) # => [Hash, Dir] +Hash # => NameError: uninitialized constant Hash +Dir # => NameError: uninitialized constant Dir + + +More specifically, +remove_class+ attempts to remove constants with the same name as the passed class objects from their parent modules. So technically this method does not guarantee the class objects themselves are not indeed valid and still alive somewhere: + + +module M + class A; end + class B < A; end +end + +A2 = M::A + +M::A.object_id # => 13053950 +Class.remove_class(M::A) + +M::B.superclass.object_id # => 13053950 (same object as before) +A2.name # => "M::A" (name is hard-coded in object) + + +WARNING: Removing fundamental classes like +String+ can result in really funky behaviour. + +The method +remove_subclasses+ provides a shortcut for removing all descendants of a given class, where "removing" has the meaning explained above: + + +class A; end +class B1 < A; end +class B2 < A; end +class C < A; end + +A.subclasses # => ["C", "B2", "B1"] +A.remove_subclasses +A.subclasses # => [] +C # => NameError: uninitialized constant C + h3. Extensions to +NilClass+ -- cgit v1.2.3