1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/delegate.rb (Delegator::public_api): take snapshot of

public method at the beginning time.

* lib/delegate.rb (SimpleDelegator#initialize): use
  Delegator.public_api since public_method might be added after
  initialization.  [ruby-dev:39383]

* lib/delegate.rb (DelegateClass): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25245 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2009-10-06 13:07:12 +00:00
parent 1c544fccd2
commit e1797bc7dd
3 changed files with 50 additions and 1 deletions

View file

@ -17,6 +17,17 @@ Tue Oct 6 06:26:00 2009 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* ext/tk/lib/tk/canvas.rb, ext/tk/lib/tkextlib/blt/component.rb: * ext/tk/lib/tk/canvas.rb, ext/tk/lib/tkextlib/blt/component.rb:
lack of support for methodcall_optkeys. lack of support for methodcall_optkeys.
Mon Oct 5 17:19:33 2009 Yukihiro Matsumoto <matz@ruby-lang.org>
* lib/delegate.rb (Delegator::public_api): take snapshot of
public method at the beginning time.
* lib/delegate.rb (SimpleDelegator#initialize): use
Delegator.public_api since public_method might be added after
initialization. [ruby-dev:39383]
* lib/delegate.rb (DelegateClass): ditto.
Mon Oct 5 12:22:12 2009 Yukihiro Matsumoto <matz@ruby-lang.org> Mon Oct 5 12:22:12 2009 Yukihiro Matsumoto <matz@ruby-lang.org>
* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro * array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro

View file

@ -196,6 +196,11 @@ class Delegator
new.__setobj__(__getobj__.dup) new.__setobj__(__getobj__.dup)
new new
end end
@delegator_api = self.public_instance_methods
def self.public_api # :nodoc:
@delegator_api
end
end end
# #
@ -228,6 +233,17 @@ class SimpleDelegator<Delegator
raise ArgumentError, "cannot delegate to self" if self.equal?(obj) raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
@delegate_sd_obj = obj @delegate_sd_obj = obj
end end
def initialize(obj) # :nodoc:
(self.public_methods - Delegator.public_api).each do |m|
class << self
self
end.class_eval do
undef_method m
end
end
super
end
end end
# :stopdoc: # :stopdoc:
@ -257,7 +273,7 @@ end
def DelegateClass(superclass) def DelegateClass(superclass)
klass = Class.new(Delegator) klass = Class.new(Delegator)
methods = superclass.public_instance_methods(true) methods = superclass.public_instance_methods(true)
methods -= ::Delegator.public_instance_methods methods -= ::Delegator.public_api
methods -= [:to_s,:inspect,:=~,:!~,:===] methods -= [:to_s,:inspect,:=~,:!~,:===]
klass.module_eval { klass.module_eval {
def __getobj__ # :nodoc: def __getobj__ # :nodoc:

View file

@ -29,4 +29,26 @@ class TestDelegateClass < Test::Unit::TestCase
simple=SimpleDelegator.new([]) simple=SimpleDelegator.new([])
assert_equal(SimpleDelegator,simple.class) assert_equal(SimpleDelegator,simple.class)
end end
class Object
def m
:o
end
end
class Foo
def m
:m
end
end
class Bar < DelegateClass(Foo)
end
def test_override
assert_equal(:o, Object.new.m)
assert_equal(:m, Foo.new.m)
assert_equal(:m, SimpleDelegator.new(Foo.new).m)
assert_equal(:m, Bar.new(Foo.new).m)
end
end end