# Finding methods of instance itself

**URL:** <https://rubytalk.org/t/finding-methods-of-instance-itself/29081>\
**Category:** ruby-talk\
**Created:** [19 July 2006 03:55 UTC](https://rubytalk.org/t/finding-methods-of-instance-itself/29081 "2006-07-19T03:55:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![List\_Recv](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@List\_Recv](https://rubytalk.org/u/List_Recv)\
**Post date:** [19 July 2006 03:55 UTC](https://rubytalk.org/t/finding-methods-of-instance-itself/29081/1 "2006-07-19T03:55:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [19 July 2006 04:25 UTC](https://rubytalk.org/t/finding-methods-of-instance-itself/29081/2 "2006-07-19T04:25:10Z")

</div>

listrecv@gmail.com 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:

&nbsp;&nbsp;object.methods(false)

and

&nbsp;&nbsp;klass.instance\_methods(false)

Also, keep in mind the differences between:

&nbsp;&nbsp;methods  
&nbsp;&nbsp;public\_methods  
&nbsp;&nbsp;private\_methods  
&nbsp;&nbsp;protected\_methods

&nbsp;&nbsp;instance\_methods  
&nbsp;&nbsp;public\_instance\_methods  
&nbsp;&nbsp;private\_instance\_methods  
&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Paul\_Battley](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@Paul\_Battley](https://rubytalk.org/u/Paul_Battley)\
**Post date:** [19 July 2006 08:10 UTC](https://rubytalk.org/t/finding-methods-of-instance-itself/29081/3 "2006-07-19T08:10:38Z")

</div>

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  
&nbsp;&nbsp;alias\_method :\_\_methods\_\_, :methods  
&nbsp;&nbsp;def methods  
&nbsp;&nbsp;&nbsp;&nbsp;return \_\_methods\_\_.sort  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def distinct\_methods  
&nbsp;&nbsp;&nbsp;&nbsp;(\_\_methods\_\_ - Object.\_\_methods\_\_).sort  
&nbsp;&nbsp;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.

> **···**
>
> On 19/07/06, listrecv@gmail.com \<listrecv@gmail.com\> 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?

---

<div class="post-metadata">

**Author:** ![List\_Recv](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@List\_Recv](https://rubytalk.org/u/List_Recv)\
**Post date:** [19 July 2006 04:45 UTC](https://rubytalk.org/t/finding-methods-of-instance-itself/29081/4 "2006-07-19T04:45:06Z")

</div>

transfire@gmail.com wrote:

> methods has a switch:
> 
> &nbsp;&nbsp;object.methods(false)
> 
> and
> 
> &nbsp;&nbsp;klass.instance\_methods(false)

Thanks!

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [19 July 2006 07:43 UTC](https://rubytalk.org/t/finding-methods-of-instance-itself/29081/5 "2006-07-19T07:43:26Z")

</div>

transfire@gmail.com schrieb:

> Also, keep in mind the differences between:
> 
> &nbsp;&nbsp;methods  
> &nbsp;&nbsp;public\_methods  
> &nbsp;&nbsp;private\_methods  
> &nbsp;&nbsp;protected\_methods
> 
> &nbsp;&nbsp;instance\_methods  
> &nbsp;&nbsp;public\_instance\_methods  
> &nbsp;&nbsp;private\_instance\_methods  
> &nbsp;&nbsp;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:

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

&nbsp;&nbsp;&nbsp;p C.instance\_methods(false) # =\> ["prot", "pub"]

Regards,  
Pit
