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

ruby-changes:31673

From: nobu <ko1@a...>
Date: Thu, 21 Nov 2013 16:33:12 +0900 (JST)
Subject: [ruby-changes:31673] nobu:r43752 (trunk): delegate.rb: try private methods after the target

nobu	2013-11-21 16:33:01 +0900 (Thu, 21 Nov 2013)

  New Revision: 43752

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

  Log:
    delegate.rb: try private methods after the target
    
    * lib/delegate.rb (Delegator#method_missing): try private methods defined in
      Kernel after the target.  [Fixes GH-449]

  Modified files:
    trunk/ChangeLog
    trunk/lib/delegate.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43751)
+++ ChangeLog	(revision 43752)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Nov 21 16:32:47 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/delegate.rb (Delegator#method_missing): try private methods defined in
+	  Kernel after the target.  [Fixes GH-449]
+
 Thu Nov 21 16:25:08 2013  Akinori MUSHA  <knu@i...>
 
 	* test/uri/test_generic.rb (URI#test_merge): Test uri + URI(path)
@@ -120,11 +125,6 @@ Wed Nov 20 01:39:02 2013  Nobuyoshi Naka https://github.com/ruby/ruby/blob/trunk/ChangeLog#L125
 	* lib/rdoc/constant.rb (RDoc::Constant#documented?): workaround for
 	  NoMethodError when the original of alias is not found.
 
-Wed Nov 20 01:27:33 2013  Nobuyoshi Nakada  <nobu@r...>
-
-	* lib/delegate.rb (Delegator#send): separate from method_missing so
-	  that super calls proper method.
-
 Tue Nov 19 23:38:49 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* configure.in (--with-os-version-style): option to transform target
@@ -287,11 +287,6 @@ Sat Nov 16 00:18:36 2013  Masaki Matsush https://github.com/ruby/ruby/blob/trunk/ChangeLog#L287
 
 	* test/ruby/test_beginendblock.rb: test for above.
 
-Fri Nov 15 17:07:31 2013  Nobuyoshi Nakada  <nobu@r...>
-
-	* lib/delegate.rb (Delegator#send): override to get rid of global function interference.
-	  [Fixes GH-449]
-
 Fri Nov 15 01:06:04 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/objspace/objspace_dump.c (dump_output): allow IO object as
Index: lib/delegate.rb
===================================================================
--- lib/delegate.rb	(revision 43751)
+++ lib/delegate.rb	(revision 43752)
@@ -46,6 +46,10 @@ class Delegator < BasicObject https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L46
     [:to_s,:inspect,:=~,:!~,:===,:<=>,:eql?,:hash].each do |m|
       undef_method m
     end
+    private_instance_methods.each do |m|
+      next if /\Ablock_given\?\z|iterator\?\z/ =~ m
+      undef_method m
+    end
   end
   include kernel
 
@@ -69,21 +73,15 @@ class Delegator < BasicObject https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L73
   def method_missing(m, *args, &block)
     target = self.__getobj__
     begin
-      target.respond_to?(m) ? target.__send__(m, *args, &block) : super(m, *args, &block)
-    ensure
-      $@.delete_if {|t| %r"\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:"o =~ t} if $@
-    end
-  end
-
-  #
-  # Handles the magic of delegation through \_\_getobj\_\_.
-  #
-  def send(m, *args, &block)
-    target = self.__getobj__
-    begin
-      target.respond_to?(m) ? target.__send__(m, *args, &block) : super(m, *args, &block)
+      if target.respond_to?(m)
+        target.__send__(m, *args, &block)
+      elsif ::Kernel.respond_to?(m, true)
+        ::Kernel.instance_method(m).bind(self).(*args, &block)
+      else
+        super(m, *args, &block)
+      end
     ensure
-      $@.delete_if {|t| %r"\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:"o =~ t} if $@
+      $@.delete_if {|t| %r"\A#{Regexp.quote(__FILE__)}:(?:#{[__LINE__-7, __LINE__-5, __LINE__-3].join('|')}):"o =~ t} if $@
     end
   end
 

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

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