Issue #14944 has been updated by sawa (Tsuyoshi Sawada).


Actually, this proposal is a duplicate of #10797

----------------------------------------
Feature #14944: Support optional inherit argument for Module#method_defined?
https://bugs.ruby-lang.org/issues/14944#change-83629

* Author: jeremyevans0 (Jeremy Evans)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Module has many introspection methods for methods and constants that
either return an array or return true or false for whether the method
or constant is defined. Most of these methods support an optional
argument that controls whether to consider inheritance.  Currently,
the following Module methods support such a argument:

* const_defined? 
* constants
* instance_methods
* private_instance_methods
* protected_instance_methods
* public_instance_methods

and the following methods do not:

* method_defined?
* private_method_defined?
* protected_method_defined?
* public_method_defined?

This patch supports such an argument for the *method_defined?
methods.

While you can currently work around the lack of support via:

~~~ ruby
mod.instance_methods(false).include?(:method_name)
~~~

This patch allows the simpler and more efficient:

~~~ ruby
mod.method_defined?(:method_name, false)
~~~
One case where you want to exclude inheritance when checking
for a method definition is when you want to replace a method
that may already exist.  To avoid a verbose warning, you want
to remove the method only if it is already defined:

~~~ ruby
remove_method(:foo) if method_defined?(:foo, false)
define_method(:foo){}
~~~

You can't call remove_method without checking for the method
definition, as that can raise a NameError, and you don't want
to include inheritance because remove_method will still raise
a NameError if the method is defined by an ancestor and not
by the module itself.

---Files--------------------------------
0001-Support-optional-inherit-argument-for-Module-method_.patch (16.5 KB)


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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>