getting method names for a Class

M

Mark Volkmann

There are so many methods in Object and Module that return arrays of
method names that it can be a bit confusing. Here's a summary of my
current understanding. Is any of this wrong?

"instance_methods" is a method from Module.
Pass it true to include inherited methods (the default) and false to
exclude them.
To get the names of public *instance* methods in the class Foo, use
Foo.instance_methods.

"methods" is a method from Object.
Pass it true to get instance methods (the default) and false to get
singleton methods.
A singleton method on a Ruby Class is essentially like a static method in J=
ava.
To get the names of public *class* methods in the class Foo, use
Foo.methods(false).

Foo.methods(false) =3D=3D Foo.singleton_methods(false)

Why doesn't Foo.methods(true) return the same thing as
Foo.instance_methods(true)?

I can see using a boolean parameter to tell whether you want inherited
methods to be included (as in the instance_methods method). However,
using a boolean parameter to tell whether you want instance or
singleton methods (as in the method "methods") seems bad. Maybe that
should be deprecated in favor of instance_methods and
singleton_methods.
 
D

David Vallner

D=C5=88a Piatok 10 Febru=C3=A1r 2006 16:46 Mark Volkmann nap=C3=ADsal:
There are so many methods in Object and Module that return arrays of
method names that it can be a bit confusing. Here's a summary of my
current understanding. Is any of this wrong?

"instance_methods" is a method from Module.
Pass it true to include inherited methods (the default) and false to
exclude them.
To get the names of public *instance* methods in the class Foo, use
Foo.instance_methods.

"methods" is a method from Object.
Pass it true to get instance methods (the default) and false to get
singleton methods.
A singleton method on a Ruby Class is essentially like a static method in
Java. To get the names of public *class* methods in the class Foo, use
Foo.methods(false).

Foo.methods(false) =3D=3D Foo.singleton_methods(false)

Why doesn't Foo.methods(true) return the same thing as
Foo.instance_methods(true)?

I can see using a boolean parameter to tell whether you want inherited
methods to be included (as in the instance_methods method). However,
using a boolean parameter to tell whether you want instance or
singleton methods (as in the method "methods") seems bad. Maybe that
should be deprecated in favor of instance_methods and
singleton_methods.

Because #instance_methods shows methods an instance of Foo will have, and=20
#methods and #singleton_methods show you methods of the class object Foo?=20
Just a guess there.

David Vallner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top