diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-02-04 12:52:08 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-02-04 12:52:08 +0000 |
commit | 23757bce574850ba88fd6a9d8e62fed3fe237da0 (patch) | |
tree | 44a60ed7c76562084f515dd56f7c29f1ad02519f /lib/delegate.rb | |
parent | b00bc5f2a83f55d1d480768b5c1fc7aa902531d4 (diff) |
* lib/delegate.rb (DelegateClass): use define_method instead of
module_eval to improve performance. [ruby-dev:33586]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15376 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/delegate.rb')
-rw-r--r-- | lib/delegate.rb | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/lib/delegate.rb b/lib/delegate.rb index 970bb9d342..c50eac2f61 100644 --- a/lib/delegate.rb +++ b/lib/delegate.rb @@ -143,7 +143,7 @@ class Delegator target.__send__(m, *args, &block) end rescue Exception - [email protected]_if{|s| /^#{__FILE__}:\d+:in `method_missing'$/ =~ s} #` + [email protected]_if{|s| %r"\A#{__FILE__}:\d+:in `method_missing'\z"o =~ s} ::Kernel::raise end end @@ -246,9 +246,17 @@ class SimpleDelegator<Delegator end # :stopdoc: -# backward compatibility ^_^;;; -Delegater = Delegator -SimpleDelegater = SimpleDelegator +def Delegator.delegating_block(mid) + lambda do |*args, &block| + begin + @delegate_dc_obj.__send__(mid, *args, &block) + rescue + re = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:/o + $!.backtrace.delete_if {|t| re =~ t} + raise + end + end +end # :startdoc: # @@ -280,19 +288,9 @@ def DelegateClass(superclass) @delegate_dc_obj = obj end } - for method in methods - begin - klass.module_eval <<-EOS, __FILE__, __LINE__+1 - def #{method}(*args, &block) - begin - @delegate_dc_obj.__send__(:#{method}, *args, &block) - rescue - raise $!, $@[2..-1] - end - end - EOS - rescue SyntaxError - raise NameError, "invalid identifier %s" % method, caller(3) + klass.module_eval do + methods.each do |method| + define_method(method, Delegator.delegating_block(method)) end end return klass |