[前][次][番号順一覧][スレッド一覧]

ruby-changes:13470

From: matz <ko1@a...>
Date: Tue, 6 Oct 2009 22:07:30 +0900 (JST)
Subject: [ruby-changes:13470] Ruby:r25245 (trunk): * lib/delegate.rb (Delegator::public_api): take snapshot of

matz	2009-10-06 22:07:12 +0900 (Tue, 06 Oct 2009)

  New Revision: 25245

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25245

  Log:
    * 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.

  Modified files:
    trunk/ChangeLog
    trunk/lib/delegate.rb
    trunk/test/test_delegate.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 25244)
+++ ChangeLog	(revision 25245)
@@ -17,6 +17,17 @@
 	* ext/tk/lib/tk/canvas.rb, ext/tk/lib/tkextlib/blt/component.rb: 
 	  lack of support for methodcall_optkeys.
 
+Mon Oct  5 17:19:33 2009  Yukihiro Matsumoto  <matz@r...>
+
+	* 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@r...>
 
 	* array.c (rb_ary_{times, shuffle_bang, sample}): reducing macro
Index: lib/delegate.rb
===================================================================
--- lib/delegate.rb	(revision 25244)
+++ lib/delegate.rb	(revision 25245)
@@ -196,6 +196,11 @@
     new.__setobj__(__getobj__.dup)
     new
   end
+
+  @delegator_api = self.public_instance_methods
+  def self.public_api   # :nodoc:
+    @delegator_api
+  end
 end
 
 #
@@ -228,6 +233,17 @@
     raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
     @delegate_sd_obj = obj
   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
 
 # :stopdoc:
@@ -257,7 +273,7 @@
 def DelegateClass(superclass)
   klass = Class.new(Delegator)
   methods = superclass.public_instance_methods(true)
-  methods -= ::Delegator.public_instance_methods
+  methods -= ::Delegator.public_api
   methods -= [:to_s,:inspect,:=~,:!~,:===]
   klass.module_eval {
     def __getobj__  # :nodoc:
Index: test/test_delegate.rb
===================================================================
--- test/test_delegate.rb	(revision 25244)
+++ test/test_delegate.rb	(revision 25245)
@@ -29,4 +29,26 @@
     simple=SimpleDelegator.new([])
     assert_equal(SimpleDelegator,simple.class)
   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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]