From a9a6c103e536bf63c24fc2c207ee63072c73a450 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 19 Nov 2013 16:27:40 +0000 Subject: [PATCH] delegate.rb: refix r43682 * lib/delegate.rb (Delegator#send): separate from method_missing so that super calls proper method. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43727 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ lib/delegate.rb | 13 ++++++++++++- test/test_delegate.rb | 9 +++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index f8fdd18f18..38385a1b60 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Nov 20 01:27:33 2013 Nobuyoshi Nakada + + * lib/delegate.rb (Delegator#send): separate from method_missing so + that super calls proper method. + Tue Nov 19 23:38:49 2013 Nobuyoshi Nakada * configure.in (--with-os-version-style): option to transform target diff --git a/lib/delegate.rb b/lib/delegate.rb index 74b13aa8a3..b929895d1f 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -74,7 +74,18 @@ class Delegator < BasicObject $@.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 diff --git a/test/test_delegate.rb b/test/test_delegate.rb index d816a62bf5..6448ef90dd 100644 --- a/test/test_delegate.rb +++ b/test/test_delegate.rb @@ -145,4 +145,13 @@ class TestDelegateClass < Test::Unit::TestCase 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