Class Module
In: lib/facet/module/redefine_method.rb
lib/facet/module/initializer.rb
lib/facet/module/methods.rb
lib/facet/module/basename.rb
lib/facet/module/redirect.rb
lib/facet/module/alias_module_function.rb
lib/facet/module/abstract.rb
lib/facet/module/wrap_method.rb
lib/facet/module/sort_attributes.rb
lib/facet/module/key_attributes.rb
lib/facet/module/by_name.rb
lib/facet/module/memoize.rb
Parent: Object

Creates a method that requires to be overridding. If it not overridden and called upon a TypeError will be raised.

 require 'facet/module/abstract'

 class C
   abstract :a
 end

 c = C.new
 c.a  #=> Error: undefined abstraction #a

Methods

Constants

METHODS_SYMBOLS = [ :inherited, :ancestors, :local, :no_acestors, :public, :private, :protected ]

Public Instance methods

Returns the root name of the module/class.

  require 'facet/module/basename'

  module Example
    class Demo
    end
  end

  Demo.name       #=> Example::Demo
  Demo.basename   #=> Demo

Note: the following documentation uses "class" because it’s more common, but it applies to modules as well.

Given the name of a class, returns the class itself (i.e. instance of Class). The dereferencing starts at Object. That is,

  Class.by_name("String")

is equivalent to

  Object.const_get("String")

The parameter name is expected to be a Symbol or String, or at least to respond to to_str.

An ArgumentError is raised if name does not correspond to an existing class. If name is not even a valid class name, the error you’ll get is not defined.

Examples:

  require 'facet/module/by_name'

  Class.by_name("String")             # -> String
  Class.by_name("::String")           # -> String
  Class.by_name("Process::Sys")       # -> Process::Sys
  Class.by_name("GorillaZ")           # -> (ArgumentError)

  Class.by_name("Enumerable")         # -> Enumerable
  Module.by_name("Enumerable")        # -> Enumerable

Automatically create an initializer assigning the given arguments.

  require 'facet/class/initializer'

  class MyClass
    attr_initializer(:a, "b", :c)
  end

_is equivalent to_

  class MyClass
    def initialize(a, b, c)
      @a,@b,@c = a,b,c
    end
  end

Downside: Initializers defined like this can’t take blocks. This can be fixed when Ruby 1.9 is out.

The initializer will not raise an Exception when the user does not supply a value for each instance variable. In that case it will just set the instance variable to nil. You can assign default values or raise an Exception in the block.

Directive for making your functions faster by trading space for time. When you "memoize" a method/function its results are cached so that later calls with the same arguments returns results in the cache instead of recalculating them.

  require 'facet/module/memoize'

  class T
    def initialize(a)
      @a = a
    end
    def a
      "#{@a ^ 3 + 4}"
    end
    memoize :a
  end

  t = T.new
  t.a.__id__  #=>
  t.a.__id__  #=>

Provides an improved method lookup routnine. It returns a list of methods according to symbol(s) given.

Usable symbols include:

  • :inherited or :ancestors
  • :local or :no_ancestors
  • :public
  • :private
  • :protected
  • :all

If no symbol is given then :public is assumed. Unrecognized symbols raise an error.

  require 'facet/module/methods'

  module Demo
    def test
      puts("Hello World!")
    end
  end

  Demo.methods(:local)    #=> ['test']

Creates a new method for a pre-existing method. If aliased is set to true (the default) then the old_method is called subsequent to calling the new definition IF the new call returns nil, otherwise the return value of the new call is returned.

This can not be used to wrap methods that take a block.

  require 'facet/module/redefine_method'

  redefine_method( sym, aliased  ) { |*args| ... }

Redirect methods to other methods. This simply defines methods by the name of a hash key which calls the method with the name of the hash’s value.

  require 'facet/module/redirect'

  class Example
    redirect :hi => :hello, :hey => :hello
    def hello(name)
      puts "Hello, #{name}."
    end
  end

  e = Example.new
  e.hello("Bob")    #=> "Hello, Bob."
  e.hi("Bob")       #=> "Hello, Bob."
  e.hey("Bob")      #=> "Hello, Bob."

The above class definition is equivalent to:

  class Example
    def hi(*args)
      hello(*args)
    end
    def hey(*args)
      hello(*args)
    end
    def hello
      puts "Hello"
    end
  end

Creates a new method wrapping the previous of the same name, passing it into the block definition of the new method.

This can not be used to wrap methods that take a block.

  require 'facet/module/wrap_method'

  wrap_method( sym ) { |old_meth, *args|
    old_meth.call
    ...
  }

Private Instance methods

Alias a module function so that the alias is also a module function. The typical alias_method does not do this.

  require 'facet/moodule/alias_module_function'

  module Demo
    module_function
    def hello
      "Hello"
    end
  end

  Demo.hello    #=> Hello

  module Demo
    alias_module_function( :hi , :hello )
  end

  Demo.hi       #=> Hello
redef( sym, aliased=true, &blk )

Alias for redefine_method

wrap( sym, &blk )

Alias for wrap_method

[Validate]