Q: introspection

Discussion in 'Ruby' started by Mike Hall, Dec 13, 2003.

  1. Mike Hall

    Mike Hall Guest

    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, Dec 13, 2003
    #1
    1. Advertising

  2. Mike Hall

    Jamis Buck Guest

    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


    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"'
     
    Jamis Buck, Dec 13, 2003
    #2
    1. Advertising

  3. Mike Hall

    Guest

    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
     
    , Dec 13, 2003
    #3
  4. Mike Hall

    Jamis Buck Guest

    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


    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"'
     
    Jamis Buck, Dec 13, 2003
    #4
  5. Mike Hall

    Mike Hall Guest

    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!
     
    Mike Hall, Dec 14, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Smith
    Replies:
    0
    Views:
    334
    David Smith
    Jul 11, 2003
  2. John Harrison

    Re: class introspection

    John Harrison, Jul 23, 2003, in forum: C++
    Replies:
    4
    Views:
    382
    John Harrison
    Jul 24, 2003
  3. Steven T. Hatton

    Introspection Guidelines for C++?

    Steven T. Hatton, Sep 3, 2004, in forum: C++
    Replies:
    0
    Views:
    627
    Steven T. Hatton
    Sep 3, 2004
  4. Steven T. Hatton

    Toward an Introspection Open Standard

    Steven T. Hatton, Oct 25, 2004, in forum: C++
    Replies:
    4
    Views:
    458
    Jonathan Turkanis
    Oct 25, 2004
  5. David C. Fox
    Replies:
    5
    Views:
    353
    Steven Taschuk
    Jul 23, 2003
Loading...

Share This Page