Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Module#method_defined?

(::interned name, ?::boolish inherit) -> bool
mod.method_defined?(symbol, inherit=true, include_all = false) → true or false
mod.method_defined?(string, inherit=true, include_all = false) → true or false

Returns true if the named method is defined by mod. If inherit is set, the lookup will also search mod’s ancestors. By default only public and protected methods are matched, but if include_all is set the lookup will also consider private methods. String arguments are converted to symbols.

Example
module A
  def method1()  end
  def protected_method1()  end
  protected :protected_method1
end
class B
  def method2()  end
  def private_method2()  end
  private :private_method2
end
class C < B
  include A
  def method3()  end
end

A.method_defined? :method1                       #=> true
C.method_defined? "method1"                      #=> true
C.method_defined? "method2"                      #=> true
C.method_defined? "method2", true                #=> true
C.method_defined? "method2", false               #=> false
C.method_defined? "method3"                      #=> true
C.method_defined? "protected_method1"            #=> true
C.method_defined? "method4"                      #=> false
C.method_defined? "private_method2"              #=> false
C.method_defined? "private_method2", true, true  #=> true
C.method_defined? "private_method2", false, true #=> false

Ruby version changes

  1. 4.1