summaryrefslogtreecommitdiff
path: root/lib/delegate.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-11-21 07:33:01 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-11-21 07:33:01 +0000
commit594eec5b7d2ca6ae1c8473b570af312dd49c40b5 (patch)
tree01184eaaddd1079fc0d9672c325c91ef087c5384 /lib/delegate.rb
parentd3edb4a85e08bbcd208f0a201f187d9ddd0a918d (diff)
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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43752 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/delegate.rb')
-rw-r--r--lib/delegate.rb26
1 files changed, 12 insertions, 14 deletions
diff --git a/lib/delegate.rb b/lib/delegate.rb
index b929895d1f..442a3762f2 100644
--- a/lib/delegate.rb
+++ b/lib/delegate.rb
@@ -46,6 +46,10 @@ class Delegator < BasicObject
[: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
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