Finding methods of instance itself

Discussion in 'Ruby' started by listrecv@gmail.com, Jul 19, 2006.

  1. Guest

    I use IRB to explore. Very often I type a.methods.sort, to see what an
    object can do. The problem is, you're hit with 100 methods from Object
    (or the superclass).

    I've experimented with:
    a.methods - a.class.superclass.methods
    which works well for simple things

    but it doesn't seem to handle cases when there are a lot of included
    modules. Any ideas on how to see the methods defined in a.class only,
    and not in any superclass or included module?
    , Jul 19, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > I use IRB to explore. Very often I type a.methods.sort, to see what an
    > object can do. The problem is, you're hit with 100 methods from Object
    > (or the superclass).
    >
    > I've experimented with:
    > a.methods - a.class.superclass.methods
    > which works well for simple things
    >
    > but it doesn't seem to handle cases when there are a lot of included
    > modules. Any ideas on how to see the methods defined in a.class only,
    > and not in any superclass or included module?


    methods has a switch:

    object.methods(false)

    and

    klass.instance_methods(false)

    Also, keep in mind the differences between:

    methods
    public_methods
    private_methods
    protected_methods

    instance_methods
    public_instance_methods
    private_instance_methods
    protected_instance_methods

    I've made a fool of myself more than once thinking the first combined
    the later three -- it does not. methods is alias of public_methods and
    likewise for the instance version.

    T.
    , Jul 19, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > methods has a switch:
    >
    > object.methods(false)
    >
    > and
    >
    > klass.instance_methods(false)


    Thanks!
    , Jul 19, 2006
    #3
  4. Pit Capitain Guest

    schrieb:
    > Also, keep in mind the differences between:
    >
    > methods
    > public_methods
    > private_methods
    > protected_methods
    >
    > instance_methods
    > public_instance_methods
    > private_instance_methods
    > protected_instance_methods
    >
    > I've made a fool of myself more than once thinking the first combined
    > the later three -- it does not.


    Me too.

    > methods is alias of public_methods and
    > likewise for the instance version.


    That's not quite correct. Kernel#methods and Module#instance_methods
    return both public and protected methods:

    class C
    public; def pub; end
    protected; def prot; end
    private; def priv; end
    end

    p C.instance_methods(false) # => ["prot", "pub"]

    Regards,
    Pit
    Pit Capitain, Jul 19, 2006
    #4
  5. Paul Battley Guest

    On 19/07/06, <> wrote:
    > I use IRB to explore. Very often I type a.methods.sort, to see what an
    > object can do. The problem is, you're hit with 100 methods from Object
    > (or the superclass).
    >
    > I've experimented with:
    > a.methods - a.class.superclass.methods
    > which works well for simple things
    >
    > but it doesn't seem to handle cases when there are a lot of included
    > modules. Any ideas on how to see the methods defined in a.class only,
    > and not in any superclass or included module?


    This isn't exactly the answer to your question, but I've defined a
    couple of methods in my .irbrc that I've found really useful. The
    first is methods aliased to methods.sort (because that's always what I
    want in irb, and the overhead doesn't matter there). The second is
    distinct_methods, which is all the methods that aren't inherited from
    Object:

    class Object
    alias_method :__methods__, :methods
    def methods
    return __methods__.sort
    end
    def distinct_methods
    (__methods__ - Object.__methods__).sort
    end
    end

    The latter generally produces a manageable list, so I haven't found
    the need to exclude methods from included modules or superclasses.

    Paul.
    Paul Battley, Jul 19, 2006
    #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. Neo
    Replies:
    1
    Views:
    516
    Scott Allen
    Jan 7, 2005
  2. John M. Gabriele
    Replies:
    18
    Views:
    1,152
    Steven Bethard
    Feb 18, 2005
  3. lightning
    Replies:
    4
    Views:
    901
    Daniel Pitts
    Oct 30, 2008
  4. Leon Bogaert
    Replies:
    19
    Views:
    324
    Robert Klemme
    Mar 23, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    312
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page