Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Q: introspection

Reply
Thread Tools

Q: introspection

 
 
Mike Hall
Guest
Posts: n/a
 
      12-13-2003
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!

 
Reply With Quote
 
 
 
 
Jamis Buck
Guest
Posts: n/a
 
      12-13-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

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"'


 
Reply With Quote
 
 
 
 
nobu.nokada@softhome.net
Guest
Posts: n/a
 
      12-13-2003
Hi,

At Sat, 13 Dec 2003 11:06:59 +0900,
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?


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

--
Nobu Nakada


 
Reply With Quote
 
Jamis Buck
Guest
Posts: n/a
 
      12-13-2003
(E-Mail Removed) wrote:

>
>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.

--
Jamis Buck
(E-Mail Removed)

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"'



 
Reply With Quote
 
Mike Hall
Guest
Posts: n/a
 
      12-14-2003
(E-Mail Removed) wrote:

>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!


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Toward an Introspection Open Standard Steven T. Hatton C++ 4 10-25-2004 09:00 PM
Introspection Guidelines for C++? Steven T. Hatton C++ 0 09-03-2004 02:57 AM
Re: class introspection John Harrison C++ 4 07-24-2003 08:45 AM
detecting containers during object introspection David C. Fox Python 5 07-23-2003 06:41 PM
Actual parameter introspection for error logging David Smith Java 0 07-11-2003 01:32 PM



Advertisments