Browse files

AS guide: documents Module#redefine_method

  • Loading branch information...
1 parent ae147b4 commit 5fc8af4712ea5b5f7a2ee8bedb3b431901de814d @fxn fxn committed Aug 10, 2010
Showing with 21 additions and 0 deletions.
  1. +21 −0 railties/guides/source/active_support_core_extensions.textile
21 railties/guides/source/active_support_core_extensions.textile
@@ -1078,6 +1078,27 @@ self.field_helpers = (FormHelper.instance_method_names - ['form_for'])
NOTE: Defined in +active_support/core_ext/module/method_names.rb+
+h4. Redefining Methods
+There are cases where you need to define a method with +define_method+, but don't know whether a method with that name already exists. If it does, a warning is issued if they are enabled. No big deal, but not clean either.
+The method +redefine_method+ prevents such a potential warning, removing the existing method before if needed. Rails uses it in a few places, for instance when it generates an association's API:
+redefine_method("#{}=") do |new_value|
+ association = association_instance_get(
+ if association.nil? || != new_value
+ association =, reflection)
+ end
+ association.replace(new_value)
+ association_instance_set(, new_value.nil? ? nil : association)
+NOTE: Defined in +active_support/core_ext/module/remove_method.rb+
h3. Extensions to +Class+
h4. Class Attributes

0 comments on commit 5fc8af4

Please sign in to comment.