List all descedents of this class.
class A ; end class B < A; end class C < A; end A.descendants #=> [B,C]
You may also limit the generational distance the subclass may be from the parent class.
class X ; end class Y < X; end class Z < Y; end X.descendants #=> [Y,Z] X.descendants(1) #=> [Y]
NOTE: This is a intensive operation. Do not expect it to be very fast.
# File lib/facets/core/facets/class/descendants.rb, line 23 def descendants(generations=-1) descendants = [] subclasses.each do |k| descendants << k if generations != 1 descendants.concat(k.descendants(generations - 1)) end end descendants end
Translate a class name to a suitable method name.
module ::Example class MethodizeExample end end Example::MethodizeExample.methodize #=> "example__methodize_example"
# File lib/facets/core/facets/class/methodize.rb, line 14 def methodize name.methodize end
Converts a class name to a unix path.
module ::Example class PathizeExample end end Example::PathizeExample.pathize #=> "example/pathize_example"
# File lib/facets/core/facets/class/pathize.rb, line 14 def pathize name.pathize end
Returns an array with the direct children of self.
Integer.subclasses # => [Fixnum, Bignum]
# File lib/facets/core/facets/class/subclasses.rb, line 17 def subclasses list = [] ObjectSpace.each_object(Class) do |c| list.unshift c if c.superclass == self end list.uniq end
Convert instatiation of a class into a Proc.
class Person def initialize(name) @name = name end def inspect @name.to_str end end persons = %w(john bob jane hans).map(&Person) persons.map{ |p| p.inspect } #=> ['john', 'bob', 'jane', 'hans']
CREDIT: Daniel Schierbeck
# File lib/facets/core/facets/class/to_proc.rb, line 20 def to_proc proc{|*args| new(*args)} end