From 7f885390876a3acdfa962e42b2bf2c73241e8702 Mon Sep 17 00:00:00 2001 From: Brad Ediger Date: Sun, 31 Jul 2011 07:38:38 -0500 Subject: [PATCH] remove_possible_method: test if method exists This speeds up remove_possible_method substantially since it doesn't have to rescue a NameError in the common case. Closes #2346. --- .../lib/active_support/core_ext/module/remove_method.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/module/remove_method.rb b/activesupport/lib/active_support/core_ext/module/remove_method.rb index 07d7c9b0183bd..b76bc16ee1183 100644 --- a/activesupport/lib/active_support/core_ext/module/remove_method.rb +++ b/activesupport/lib/active_support/core_ext/module/remove_method.rb @@ -1,11 +1,16 @@ class Module def remove_possible_method(method) - remove_method(method) + if method_defined?(method) || private_method_defined?(method) + remove_method(method) + end rescue NameError + # If the requested method is defined on a superclass or included module, + # method_defined? returns true but remove_method throws a NameError. + # Ignore this. end def redefine_method(method, &block) remove_possible_method(method) define_method(method, &block) end -end \ No newline at end of file +end