Class | Class
In: | lib/core/facets/class/descendants.rb lib/core/facets/class/methodize.rb lib/core/facets/class/pathize.rb lib/core/facets/class/subclasses.rb lib/core/facets/class/to_proc.rb lib/core/facets/module/revise.rb lib/core-uncommon/facets/class/preallocate.rb lib/core-uncommon/facets/class/singleton.rb lib/core-uncommon/facets/module/class_extend.rb
Parent: | Object

### Methods

class_extend descendants methodize pathize preallocate singleton? subclasses to_proc

### Public Instance methods

class_extend(*mods, &block)

For Class, Module#class_extend is similar to class_eval.

The alternative is to "undef_method :class_extend", but this seems uneccessarily limited.

descendants(generations=-1)

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.

methodize()

Translate a class name to a suitable method name.

      module ::Example
        class MethodizeExample
        end
      end

      Example::MethodizeExample.methodize  #=> "example__methodize_example"

pathize()

Converts a class name to a unix path.

      module ::Example
        class PathizeExample
        end
      end

      Example::PathizeExample.pathize  #=> "example/pathize_example"

preallocate(aspect)

Designate aspect modules to be added to a object at instantiation.

      class Firetruck
        def put_out_fire(option)
          "Put out #{option}"
        end
      end

      module FastFiretruck
        def put_out_fire(option)
          super("very #{option}!")
        end
      end

      Firetruck.preallocate(FastFiretruck)

      ft = Firetruck.new
      ft.put_out_fire('fast') #=> "Put out very fast!"

This method is very similar to the idea of prepend, but it has some limitations in that it works by overriding new and allocate and extends an object with the aspect modules on instantiation. A true prepend implementation would not have to do this —but would be a natural part of the class heirarchy instead. For this reason, this method has been named preallocate, rather than prepend.

NOTE: This is not (presently) a common core extension and is not loaded automatically when using `require ‘facets‘`.

CREDIT: Trans

singleton?()
subclasses()

Returns an array with the direct children of `self`.

      Integer.subclasses # => [Fixnum, Bignum]

to_proc()

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

[Validate]