John Duff John Duff - 7 months ago 5
Ruby Question

Why doesn't Module.method_defined?(:method) work correctly?

I'm trying to check if a method is defined in a module using

Module.method_defined?(:method)
and it is returning false it should be returing true.

module Something
def self.another
1
end
end


Something.methods
has 'another' listed but
Something.method_defined?(:another)
returns
false
.

Is this maybe not working because the method is defined on self? If this is the case is there another way to check if the method is defined on the module other than using
method_defined?
?

Answer

To know whether the module has a module method, you can use respond_to? on the module:

Something.respond_to?(another)
=> true

method_defined? will tell you whether INSTANCES of the class with the module included responds to the given method.

Comments