diff options
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/core_ext/object_and_class.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/activesupport/lib/core_ext/object_and_class.rb b/activesupport/lib/core_ext/object_and_class.rb new file mode 100644 index 0000000000..59a463ae29 --- /dev/null +++ b/activesupport/lib/core_ext/object_and_class.rb @@ -0,0 +1,24 @@ +class Object #:nodoc: + def remove_subclasses_of(superclass) + subclasses_of(superclass).each { |subclass| Object.send(:remove_const, subclass) rescue nil } + end + + def subclasses_of(superclass) + subclasses = [] + ObjectSpace.each_object(Class) do |k| + next if !k.ancestors.include?(superclass) || superclass == k || k.to_s.include?("::") || subclasses.include?(k.to_s) + subclasses << k.to_s + end + subclasses + end +end + +class Class #:nodoc: + def remove_subclasses + Object.remove_subclasses_of(self) + end + + def subclasses + Object.subclasses_of(self) + end +end |