summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-02-04 12:52:08 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-02-04 12:52:08 +0000
commit23757bce574850ba88fd6a9d8e62fed3fe237da0 (patch)
tree44a60ed7c76562084f515dd56f7c29f1ad02519f
parentb00bc5f2a83f55d1d480768b5c1fc7aa902531d4 (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
-rw-r--r--ChangeLog5
-rw-r--r--lib/delegate.rb32
2 files changed, 20 insertions, 17 deletions
diff --git a/ChangeLog b/ChangeLog
index c8f843082b..24ad19d3f5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Mon Feb 4 21:52:06 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * lib/delegate.rb (DelegateClass): use define_method instead of
+ module_eval to improve performance. [ruby-dev:33586]
+
Mon Feb 4 16:44:24 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* configure.in (darwin): NSIG is not defined if _XOPEN_SOURCE > 500L.
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
- $@.delete_if{|s| /^#{__FILE__}:\d+:in `method_missing'$/ =~ s} #`
+ $@.delete_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