From a50cc9ce082a9e356d0bc188785973ebbd0529ff Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 3 Jan 2017 09:35:26 +0000 Subject: [PATCH] forwardable.rb: use defined? * lib/forwardable.rb (_delegator_method): use defined? operator instead of binding and calling unbound Kernel#respond_to?. a remedy for an performance bottleneck. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57256 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/forwardable.rb | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/forwardable.rb b/lib/forwardable.rb index cc5ac15001..ef2cf74370 100644 --- a/lib/forwardable.rb +++ b/lib/forwardable.rb @@ -130,12 +130,13 @@ module Forwardable # delegate [method, method, ...] => accessor # def instance_delegate(hash) - hash.each{ |methods, accessor| - methods = [methods] unless defined?(methods.each) - methods.each{ |method| - def_instance_delegator(accessor, method) - } - } + hash.each do |methods, accessor| + if defined?(methods.each) + def_instance_delegator(accessor, methods) + else + methods.each {|method| def_instance_delegator(accessor, method)} + end + end end # @@ -261,12 +262,13 @@ module SingleForwardable # delegate [method, method, ...] => accessor # def single_delegate(hash) - hash.each{ |methods, accessor| - methods = [methods] unless defined?(methods.each) - methods.each{ |method| - def_single_delegator(accessor, method) - } - } + hash.each do |methods, accessor| + if defined?(methods.each) + def_single_delegator(accessor, methods) + else + methods.each {|method| def_single_delegator(accessor, method)} + end + end end #