Feature #15842 ยป delegate-class-block.patch
| lib/delegate.rb | ||
|---|---|---|
|
# end
|
||
|
# end
|
||
|
#
|
||
|
# or:
|
||
|
#
|
||
|
# MyClass = DelegateClass(ClassToDelegateTo) do # Step 1
|
||
|
# def initialize
|
||
|
# super(obj_of_ClassToDelegateTo) # Step 2
|
||
|
# end
|
||
|
# end
|
||
|
#
|
||
|
# Here's a sample of use from Tempfile which is really a File object with a
|
||
|
# few special rules about storage location and when the File should be
|
||
|
# deleted. That makes for an almost textbook perfect example of how to use
|
||
| ... | ... | |
|
# # ...
|
||
|
# end
|
||
|
#
|
||
|
def DelegateClass(superclass)
|
||
|
def DelegateClass(superclass, &block)
|
||
|
klass = Class.new(Delegator)
|
||
|
methods = superclass.instance_methods
|
||
|
methods -= ::Delegator.public_api
|
||
| ... | ... | |
|
klass.define_singleton_method :protected_instance_methods do |all=true|
|
||
|
super(all) | superclass.protected_instance_methods
|
||
|
end
|
||
|
klass.module_eval(&block) if block
|
||
|
return klass
|
||
|
end
|
||
| test/test_delegate.rb | ||
|---|---|---|
|
assert_equal(:m, obj.m, "[ruby-dev:33116]")
|
||
|
end
|
||
|
def test_delegate_class_block
|
||
|
klass = DelegateClass(Array) do
|
||
|
alias foo first
|
||
|
end
|
||
|
assert_equal(1, klass.new([1]).foo)
|
||
|
end
|
||
|
def test_systemcallerror_eq
|
||
|
e = SystemCallError.new(0)
|
||
|
assert((SimpleDelegator.new(e) == e) == (e == SimpleDelegator.new(e)), "[ruby-dev:34808]")
|
||