3
# Unassociates the class with its subclasses and removes the subclasses
6
# Integer.remove_subclasses # => [Bignum, Fixnum]
7
# Fixnum # => NameError: uninitialized constant Fixnum
9
Object.remove_subclasses_of(self)
12
# Returns an array with the names of the subclasses of +self+ as strings.
14
# Integer.subclasses # => ["Bignum", "Fixnum"]
16
Object.subclasses_of(self).map { |o| o.to_s }
19
# Removes the classes in +klasses+ from their parent module.
21
# Ordinary classes belong to some module via a constant. This method computes
22
# that constant name from the class name and removes it from the module it
25
# Object.remove_class(Integer) # => [Integer]
26
# Integer # => NameError: uninitialized constant Integer
28
# Take into account that in general the class object could be still stored
31
# i = Integer # => Integer
32
# Object.remove_class(Integer) # => [Integer]
33
# Integer # => NameError: uninitialized constant Integer
34
# i.subclasses # => ["Bignum", "Fixnum"]
35
# Fixnum.superclass # => Integer
36
def remove_class(*klasses)
37
klasses.flatten.each do |klass|
38
# Skip this class if there is nothing bound to this name
39
next unless defined?(klass.name)
41
basename = klass.to_s.split("::").last
44
# Skip this class if it does not match the current one bound to this name
45
next unless parent.const_defined?(basename) && klass = parent.const_get(basename)
47
parent.instance_eval { remove_const basename } unless parent == klass