Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Created October 8, 2014 14:19
Show Gist options
  • Save ifyouseewendy/6bb1435ef7205c4ac3a4 to your computer and use it in GitHub Desktop.
Save ifyouseewendy/6bb1435ef7205c4ac3a4 to your computer and use it in GitHub Desktop.
Get subclasses, using Iterator Pattern
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
subclasses_of(Numeric)
# => ["Complex", "Rational", "Bignum", "Float", "Fixnum", "Integer"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment