Credit:Daniel Schierbeck
Methods
Public Instance methods
cattr( *syms )

Creates a class-variable attribute that can be accessed both on an instance and class level.

NOTE These used to be a Module methods. But turns out these to work as expected when included. The class-level method is not carried along. So the are now just class methods. Accordingly, mattr will eventually be deprecated so use cattr instead.

This method is also aliased as mattr
# File lib/facets/core/class/cattr.rb, line 16
  def cattr( *syms )
    accessors, readers = syms.flatten.partition { |a| a.to_s =~ /=$/ }
    writers = accessors.collect{ |e| e.to_s.chomp('=').to_sym }
    readers.concat( writers )
    cattr_writer( *writers )
    cattr_reader( *readers )
    return readers + accessors
  end
cattr_accessor(*syms)

Creates a class-variable attr_accessor that can be accessed both on an instance and class level.

  class MyClass
    cattr_accessor :a
  end

  MyClass.a = 10
  MyClass.a           #=> 10
  mc = MyClass.new
  mc.a                #=> 10
This method is also aliased as mattr_accessor
# File lib/facets/core/class/cattr.rb, line 96
  def cattr_accessor(*syms)
    m = []
    m.concat( cattr_reader(*syms) )
    m.concat( cattr_writer(*syms) )
    m
  end
cattr_reader( *syms )

Creates a class-variable attr_reader that can be accessed both on an instance and class level.

  class MyClass
    @@a = 10
    cattr_reader :a
  end

  MyClass.a           #=> 10
  MyClass.new.a       #=> 10
This method is also aliased as mattr_reader
# File lib/facets/core/class/cattr.rb, line 37
  def cattr_reader( *syms )
    syms.flatten.each do |sym|
      class_eval("def self.\#{sym}\n@@\#{sym}\nend\ndef \#{sym}\n@@\#{sym}\nend\n", __FILE__, __LINE__)
    end
    return syms
  end
cattr_writer(*syms)

Creates a class-variable attr_writer that can be accessed both on an instance and class level.

  class MyClass
    cattr_writer :a
    def a
      @@a
    end
  end

  MyClass.a = 10
  MyClass.a            #=> 10
  MyClass.new.a = 29
  MyClass.a            #=> 29
This method is also aliased as mattr_writer
# File lib/facets/core/class/cattr.rb, line 68
  def cattr_writer(*syms)
    syms.flatten.each do |sym|
      class_eval("def self.\#{sym}=(obj)\n@@\#{sym} = obj\nend\ndef \#{sym}=(obj)\n@@\#{sym}=(obj)\nend\n", __FILE__, __LINE__)
    end
    return syms
  end
descendents()
This method is also aliased as subclasses
# File lib/facets/core/class/descendents.rb, line 7
  def descendents
    subclass = []
    ObjectSpace.each_object( Class ) do |c|
      if c.ancestors.include?( self ) and self != c
        subclass << c
      end
    end
    return subclass
  end
mattr( *syms )

Alias for cattr

mattr_accessor(*syms)

Alias for cattr_accessor

mattr_reader( *syms )

Alias for cattr_reader

mattr_writer(*syms)

Alias for cattr_writer

method_name(

Converts a class name to a suitable method name

  My::ClassName.method_name => "my__class_name"
# File lib/facets/core/class/method_name.rb, line 8
  def method_name # inspired by facet/string/underscore
    name.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').gsub('::','__').downcase
  end
remove_descendents()
This method is also aliased as remove_subclasses
# File lib/facets/core/class/remove_descendents.rb, line 9
  def remove_descendents
    self.descendents.each do |subclass|
      Object.send(:remove_const, subclass.name) rescue nil
    end
    ObjectSpace.garbage_collect
  end
remove_subclasses()

Alias for remove_descendents

subclasses()

Alias for descendents

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

  %w(john bob jane hans).map(&Person) => [john, bob, jane, hans]
# File lib/facets/core/class/to_proc.rb, line 21
  def to_proc
    proc{|*args| new(*args)}
  end
unix_path()

Converts a class name to a unix path

  My::ClassName.unix_path => "/my/class_name"

Inspired by facet/string/underscore.

# File lib/facets/core/class/unix_path.rb, line 10
  def unix_path
    '/' + name.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').gsub('::','/').downcase
  end