lib/clevic/extensions.rb in clevic-0.13.0.b6 vs lib/clevic/extensions.rb in clevic-0.13.0.b9

- old
+ new

@@ -12,9 +12,28 @@ # pass self to the block and return the results of the block. def with( &block ) yield( self ) end + + # return a list of the subclasses of a class + # from Matt Williams + # http://matthewkwilliams.com/index.php/2008/08/22/rubys-objectspace-subclasses/ + def self.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 end class String # just grab the character code of the last character in the string # TODO this won't work in unicode or utf-8