[Pdx-pm] Getting a list of methods that a package responds to

Phil Tomson ptkwt at aracnet.com
Mon Aug 22 14:38:29 PDT 2005



On Mon, 22 Aug 2005, Kyle Dawkins wrote:

> Hi all
> 
> Sorry for asking this dopey question, but I am having some weird  
> problems surfing the symbol tables of some of my classes, and I can't  
> figure out what's going on.
> 
> The basic idea is to just get a list of methods that a package  
> responds to (including inherited methods).  Basically, when an  
> instance of a class is initialised,  I do something like this:
> 
> 
> my $package = ref($self);
> my %symbolTable = eval("%${package}::");
> 
> foreach my $symbol (keys %symbolTable) {
>      ...
> }
> 
> because I am looking for certain methods to "register" for use  
> elsewhere.  However, this doesn't work; it kinda works, but it never  
> returns a complete list of methods; in fact, it returns *different*  
> lists of methods for successive calls.
> 

  object.methods #=> returns array of method names


And since Classes and Modules are objects you can call the methods method 
on them as well (though you may want to call 'instance_methods' depending 
on what you're looking for).

You want'em sorted?
  object.methods.sort

Looking for a particular method?

  object.include? "methodname"  #=> returns true of false


Oh, wait ...oops, sorry wrong list ;-)

Phil



More information about the Pdx-pm-list mailing list