Skip to content

Instantly share code, notes, and snippets.

@dbrady
Created March 15, 2012 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbrady/2046988 to your computer and use it in GitHub Desktop.
Save dbrady/2046988 to your computer and use it in GitHub Desktop.
Return the list of descendants of a class
class Class
def extend?(klass)
superclass && (superclass == klass || superclass.extend?(klass))
end
def descendants
Module.constants.select do |constant_name|
constant = eval(constant_name.to_s)
if constant && constant.is_a?(Class) && constant.extend?(self)
constant
end
end
end
end
# ruby-1.9.2-p290 :002 > StandardError.descendants
# => [:TypeError, :ArgumentError, :IndexError, :KeyError, :RangeError, :NameError, :NoMethodError, :RuntimeError, :EncodingError, :SystemCallError, :ZeroDivisionError, :FloatDomainError, :RegexpError, :IOError, :EOFError, :LocalJumpError, :StopIteration, :ThreadError, :FiberError]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment