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

ruby-changes:31648

From: nobu <ko1@a...>
Date: Wed, 20 Nov 2013 01:27:48 +0900 (JST)
Subject: [ruby-changes:31648] nobu:r43727 (trunk): delegate.rb: refix r43682

nobu	2013-11-20 01:27:40 +0900 (Wed, 20 Nov 2013)

  New Revision: 43727

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

  Log:
    delegate.rb: refix r43682
    
    * lib/delegate.rb (Delegator#send): separate from method_missing so
      that super calls proper method.

  Modified files:
    trunk/ChangeLog
    trunk/lib/delegate.rb
    trunk/test/test_delegate.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43726)
+++ ChangeLog	(revision 43727)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+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
Index: lib/delegate.rb
===================================================================
--- lib/delegate.rb	(revision 43726)
+++ lib/delegate.rb	(revision 43727)
@@ -74,7 +74,18 @@ class Delegator < BasicObject https://github.com/ruby/ruby/blob/trunk/lib/delegate.rb#L74
       $@.delete_if {|t| %r"\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:"o =~ t} if $@
     end
   end
-  alias send method_missing
+
+  #
+  # 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)
+    ensure
+      $@.delete_if {|t| %r"\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:"o =~ t} if $@
+    end
+  end
 
   #
   # Checks for a method provided by this the delegate object by forwarding the
Index: test/test_delegate.rb
===================================================================
--- test/test_delegate.rb	(revision 43726)
+++ test/test_delegate.rb	(revision 43727)
@@ -145,4 +145,13 @@ class TestDelegateClass < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/test_delegate.rb#L145
     assert_nothing_raised(ArgumentError) {d.open}
     assert_nothing_raised(ArgumentError) {d.send(:open)}
   end
+
+  def test_send_method_in_delegator
+    d = Class.new(SimpleDelegator) do
+      def foo
+        "foo"
+      end
+    end.new(Object.new)
+    assert_equal("foo", d.send(:foo))
+  end
 end

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

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