Forum: Ruby method_defined? doesn't see module methods?

Posted by matt neuburg (Guest)
on 2008-01-27 18:47
(Received via mailing list)
module M
  def self.howdy
    "hello"
  end
end
p M.method_defined? :howdy #=> false

What question should I ask in order to get "true"? Thx - m.
Posted by Stefano Crocco (crocco)
on 2008-01-27 20:34
(Received via mailing list)
Alle Sunday 27 January 2008, matt neuburg ha scritto:
> module M
>   def self.howdy
>     "hello"
>   end
> end
> p M.method_defined? :howdy #=> false
>
> What question should I ask in order to get "true"? Thx - m.

According to the ri documentation, method_defined? tells wether 
instances of
the class, or of a class including the module, will respond to the given
method. In other words :

Cls.method_defined?(:a_method) == true

means that

Cls.new.respond_to?(:a_method) == true

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

M.respond_to?(:howdy)
=> true

I hope this helps

Stefano
Posted by matt neuburg (Guest)
on 2008-01-27 22:10
(Received via mailing list)
Stefano Crocco <stefano.crocco@alice.it> wrote:

> To know whether the module has a module method, you can use respond_to? on the
> module:
> 
> M.respond_to?(:howdy)
> => true

Excellent, thanks! m.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.