Q: introspection

M

Mike Hall

Is there a way to find out the methods that a given class/modules provides,
without including the methods from Object, Kernel, etc, and without deleting
any commonly named methods?

For example, the Dir class, (Dir.methods - Object.methods) will remove the 'new'
method, and the 'open' method gets tossed if we subract out the Kernel.methods.

Is this possible with the current set of 'public_methods' and 'private_methods' et.al?

Thanks!
 
J

Jamis Buck

Mike said:
Is there a way to find out the methods that a given class/modules provides,
without including the methods from Object, Kernel, etc, and without deleting
any commonly named methods?

For example, the Dir class, (Dir.methods - Object.methods) will remove the 'new'
method, and the 'open' method gets tossed if we subract out the Kernel.methods.

Is this possible with the current set of 'public_methods' and 'private_methods' et.al?

Thanks!

I'd be interested in this, too. I typically do (
MyClass.instance_methods - MyClass.superclass.instance_methods ), but
this throws out any that MyClass redefines, which is bad. To get around
this I've been forced to parse the inspection string of UnboundMethod:

methods = MyClass.instance_methods.find_all do |name|
MyClass.instance_method( name ).inspect =~ /MyClass#/
end

As for not deleting any "commonly named" methods... I guess I'm not sure
what you mean there. I guess you could define an array of method names
that you don't want deleted, and add them to the loop. But the system
itself has no way of knowing that you WANT the 'open' method of Dir (for
instance) unless you specify some kind of search criteria that include it.

BTW, a method of Method and UnboundMethod that tells you the name of the
class that most recently defined the method would be very handy. Is
there one, and I just haven't seen it?
 
N

nobu.nokada

Hi,

At Sat, 13 Dec 2003 11:06:59 +0900,
Mike said:
Is there a way to find out the methods that a given class/modules provides,
without including the methods from Object, Kernel, etc, and without deleting
any commonly named methods?

For example, the Dir class, (Dir.methods - Object.methods) will remove the 'new'
method, and the 'open' method gets tossed if we subract out the Kernel.methods.

Is this possible with the current set of 'public_methods' and 'private_methods' et.al?

In 1.8, public_methods et al take an optional argument which
directs that inherited methods are included or not.

$ ruby -rpp -e 'pp Dir.public_methods(false)'
["glob",
"superclass",
"entries",
"pwd",
"[]",
"foreach",
"open",
"rmdir",
"delete",
"getwd",
"new",
"mkdir",
"chdir",
"allocate",
"chroot",
"unlink"]
 
J

Jamis Buck

In 1.8, public_methods et al take an optional argument which
directs that inherited methods are included or not.

Now that's handy. I only wish I'd known about it a month ago. ;)

I was about to ask if these were documented somewhere, and just noticed
that ri says the same thing about instance_methods (ie, you can give it
a boolean indicating whether you want inherited methods or not). Good
to know.
 
M

Mike Hall

In 1.8, public_methods et al take an optional argument which
directs that inherited methods are included or not.

Thanks much!
Time for me to go read the docs some more!
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top