ruby-changes:3887
From: ko1@a...
Date: Mon, 4 Feb 2008 21:52:43 +0900 (JST)
Subject: [ruby-changes:3887] nobu - Ruby:r15376 (trunk): * lib/delegate.rb (DelegateClass): use define_method instead of
nobu 2008-02-04 21:52:08 +0900 (Mon, 04 Feb 2008) New Revision: 15376 Modified files: trunk/ChangeLog trunk/lib/delegate.rb Log: * lib/delegate.rb (DelegateClass): use define_method instead of module_eval to improve performance. [ruby-dev:33586] http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/lib/delegate.rb?r1=15376&r2=15375&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15376&r2=15375&diff_format=u Index: ChangeLog =================================================================== --- ChangeLog (revision 15375) +++ ChangeLog (revision 15376) @@ -1,3 +1,8 @@ +Mon Feb 4 21:52:06 2008 Nobuyoshi Nakada <nobu@r...> + + * 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@r...> * configure.in (darwin): NSIG is not defined if _XOPEN_SOURCE > 500L. Index: lib/delegate.rb =================================================================== --- lib/delegate.rb (revision 15375) +++ lib/delegate.rb (revision 15376) @@ -143,7 +143,7 @@ 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 @@ 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 @@ @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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/