delegate.rb

Path: delegate.rb
Last Update: Sun Apr 11 16:57:33 GMT+10:00 2004
 Delegation class that delegates even methods defined in super class,

which can not be covered with normal method_missing hack.

 Delegator is the abstract delegation class. Need to redefine

`getobj’ method in the subclass. SimpleDelegator is the concrete subclass for simple delegation.

Usage:

  foo = Object.new
  foo2 = SimpleDelegator.new(foo)
  foo.hash == foo2.hash # => false

  Foo = DelegateClass(Array)

 class ExtArray<DelegateClass(Array)
   ...
 end

Methods

Constants

Delegater = Delegator
  backward compatibility ^_^;;;
SimpleDelegater = SimpleDelegator

Classes and Modules

Class Delegator
Class ExtArray
Class SimpleDelegator

Public Instance methods

[Source]

# File delegate.rb, line 86
def DelegateClass(superclass)
  klass = Class.new
  methods = superclass.public_instance_methods(true)
  methods -= ::Kernel.public_instance_methods(false)
  methods |= ["to_s","to_a","inspect","==","=~","==="]
  klass.module_eval "def initialize(obj)\n@_dc_obj = obj\nend\ndef __getobj__\n@_dc_obj\nend\n"
  for method in methods
    begin
      klass.module_eval "def \#{method}(*args, &block)\nbegin\n@_dc_obj.__send__(:\#{method}, *args, &block)\nrescue\n$@[0,2] = nil\nraise\nend\nend\n"
    rescue SyntaxError
      raise NameError, "invalid identifier %s" % method, caller(3)
    end
  end
  return klass
end

[Validate]