1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
nobu 2008-02-04 12:52:08 +00:00
parent b00bc5f2a8
commit 23757bce57
2 changed files with 20 additions and 17 deletions

View file

@ -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.

View file

@ -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