Nobuyoshi Nakada
2/13/2008 12:42:00 PM
Hi,
At Wed, 13 Feb 2008 19:58:56 +0900,
Todd Benson wrote in [ruby-talk:290915]:
> > How can I test if an instance method is defined for a class, without
> > having to create an instance of that class?
> >
> > Simple example: Test if 'length' defined for the 'String' class?
> >
> >
> > Best regards,
> >
> > Jari Williamsson
>
> I don't know if it's the best way, but there is #instance_methods
>
> irb(main):008:0> Fixnum.instance_methods.grep /ins/
> =>["inspect", "instance_variable_defined?", "instance_variables",
> "instance_variable_get", "instance_of?", "instance_eval",
> "instance_variable_set"]
It's nice to find all matching methods, but inefficient when
you know the exact name what you want to know.
$ ruby -e 'p String.method_defined?(:length)'
true
--
Nobu Nakada