### Home Classes Methods

### In Files

- lib/facets/class/subclasses.rb
- lib/facets/class/singleton.rb
- lib/facets/class/descendants.rb
- lib/facets/class/preallocate.rb
- lib/facets/class/methodize.rb
- lib/facets/class/to_proc.rb
- lib/facets/class/pathize.rb
- lib/facets/module/revise.rb
- lib/facets/module/class_extend.rb

### Parent

Object

### Methods

- #class_extend
- #descendants
- #methodize
- #pathize
- #preallocate
- #singleton?
- #subclasses
- #to_proc

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Lazy
- Lazy::Future
- Lazy::Promise
- Lazy::Promise::DIVERGES
- Lazy::PromiseSafe
- Enumerable
- Enumerable::Recursor
- Method
- Method::Composition
- Module
- Module::InstanceFunction
- Array
- Binding
- Class
- Comparable
- Denumerable
- Denumerator
- Dir
- Enumerator
- Exception
- Facets
- FalseClass
- File
- FileTest
- Fixnum
- Float
- Functor
- Hash
- Indexable
- Instance
- Integer
- Kernel
- MatchData
- NA
- NilClass
- Numeric
- Object
- ObjectSpace
- Proc
- Process
- Range
- Regexp
- String
- Struct
- Symbol
- Time
- TrueClass
- UnboundMethod

No matching classes.

# Class

### Public Instance Methods

class_extend(*mods, &block) click to toggle source

For Class, Module#class_extend is similar to class_eval.

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

@uncommon

    require 'facets/module/class_extend'

    # File lib/facets/module/class_extend.rb, line 84
    def class_extend(*mods, &block)
      class_extension = Module.new
      class_extension.__send__(:include, *mods)
      class_extension.module_eval(&block) if block
      extend(class_extension)
      class_extensions << class_extension
    end

descendants(generations=-1) click to toggle source

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.

@author Roger Pack

    # File lib/facets/class/descendants.rb, line 24
    def descendants(generations=-1)
      descendants = []
      subclasses.each do |k|
        descendants << k
        if generations != 1
          descendants.concat(k.descendants(generations - 1))
        end
      end
      descendants
    end

methodize() click to toggle source

Translate a class name to a suitable method name.

    module ::Example
      class MethodizeExample
      end
    end

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

    # File lib/facets/class/methodize.rb, line 14
    def methodize
      name.methodize
    end

pathize() click to toggle source

Converts a class name to a unix path.

    module ::Example
      class PathizeExample
      end
    end

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

    # File lib/facets/class/pathize.rb, line 14
    def pathize
      name.pathize
    end

preallocate(aspect) click to toggle source

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 method is not a common core extension and is not loaded automatically when using `require 'facets'`.

CREDIT: Trans

@uncommon

    require 'facets/class/preallocate'

    # File lib/facets/class/preallocate.rb, line 37
    def preallocate(aspect)
      _new      = method(:new)
      _allocate = method(:allocate)
      (class << self; self; end).class_eval do
        define_method(:new) do |*args|
          o = _new.call(*args)
          o.extend aspect
          o
        end
        define_method(:allocate) do |*args|
          o = _allocate.call(*args)
          o.extend aspect
          o
        end
      end
    end

singleton?() click to toggle source

Return true if a class is the singleton class of an object.

@uncommon

    require 'facets/class/singleton'

    # File lib/facets/class/singleton.rb, line 9
    def singleton?
      ! ancestors.include?( self ) rescue false
    end

subclasses() click to toggle source

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

    Integer.subclasses # => [Fixnum, Bignum]

    # File lib/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

to_proc() click to toggle source

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/class/to_proc.rb, line 20
    def to_proc
      proc{|*args| new(*args)}
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.