Module: Subclasses

Defined in:
lib/ruby_extensions.rb

Instance Method Summary collapse

Instance Method Details

#subclasses(direct = false) ⇒ Object

return a list of the subclasses of a class


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_extensions.rb', line 39

def subclasses(direct = false)
  classes = []
  if direct
    ObjectSpace.each_object(Class) do |c|
      next unless c.superclass == self
      classes << c
    end
  else
    ObjectSpace.each_object(Class) do |c|
      next unless c.ancestors.include?(self) and (c != self)
      classes << c
    end
  end
  classes
end