Issue #5673 has been updated by trans (Thomas Sawyer).


B/c often times it's not an error. Cases such as undefining method before redefining new one to suppress warning message of overriden method. Or different versions of a library might get used where one has such method and another does not.

If we need to remove a method from a class/module that may or may not have the method defined, it's less optimal. We either have do something like:

  if instance_methods(false).include?(:foo)
    undef_method(:foo)
  end

Or,

  begin
    undef_method(:foo)
  rescue NameError
  end

Both of which entail more overhead and considerations than is really necessary.

On the other hand, if it did not raise an error and returned true/false instead, then it is easy enough for us to handle the error if it truly matters.

  success = undef_method(:foo)
 
  raise NameError, "undefined method `foo' for class `#{self}'" unless success

#undef_method is not something that's used very frequently, so I think code improvements for these cases is worth it.

----------------------------------------
Feature #5673: undef_method probably doesn't need to raise an error
https://bugs.ruby-lang.org/issues/5673#change-25550

Author: trans (Thomas Sawyer)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


Is there any significant reason for #undef_method to raise an error if the method is already undefined? If no, then change it to just continue on. It can return true/false to relay if was undefined vs already undefined.



-- 
http://bugs.ruby-lang.org/