Skip to content
Browse files

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.
  • Loading branch information...
1 parent 4c85c4e commit 7f885390876a3acdfa962e42b2bf2c73241e8702 @bradediger bradediger committed Jul 31, 2011
Showing with 7 additions and 2 deletions.
  1. +7 −2 activesupport/lib/active_support/core_ext/module/remove_method.rb
9 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.
def redefine_method(method, &block)
define_method(method, &block)

0 comments on commit 7f88539

Please sign in to comment.
Something went wrong with that request. Please try again.