Q: introspection

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!

Mike Hall wrote:

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?

···


Jamis Buck
jgb3@email.byu.edu

ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

Hi,

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”]

···

At Sat, 13 Dec 2003 11:06:59 +0900, Mike Hall wrote:


Nobu Nakada

Now that’s handy. I only wish I’d known about it a month ago. :wink:

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.

···

nobu.nokada@softhome.net wrote:

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


Jamis Buck
jgb3@email.byu.edu

ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

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

···

nobu.nokada@softhome.net wrote:

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