Sha256: 0e4528af3a7c702144d7b4a09d9ffab99d36691ab448fa24a4ada6990b36159c
Contents?: true
Size: 1.28 KB
Versions: 3
Compression:
Stored size: 1.28 KB
Contents
require "reflexive/core_ext/kernel/singleton_class" module Reflexive module_function # List all descedents of this class. # # class X ; end # class A < X; end # class B < X; end # X.descendents #=> [A,B] # # You may also limit the generational distance # the subclass may be from the parent class. # # class X ; end # class A < X; end # class B < A; end # X.descendents #=> [A, B] # X.descendents(1) #=> [A] # # NOTE: This is a intensive operation. Do not # expect it to be super fast. def descendants(klass, generations=nil) subclass = [] ObjectSpace.each_object(Class) do |c| next if c == klass ancestors = c.ancestors[0 .. (generations || -1)] subclass << c if ancestors.include?(klass) ancestors = c.singleton_class.ancestors[0 .. (generations || -1)] subclass << c if ancestors.include?(klass) end if klass.instance_of?(Module) # descendants of module are also modules which include the module ObjectSpace.each_object(Module) do |c| next if c == klass ancestors = c.ancestors[0 .. (generations || -1)] subclass << c if ancestors.include?(klass) end end subclass.uniq end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
reflexive-0.1.2 | lib/reflexive/descendants.rb |
reflexive-0.1.1 | lib/reflexive/descendants.rb |
reflexive-0.1.0 | lib/reflexive/descendants.rb |