# Finding methods of instance itself

**URL:** <https://www.ruby-forum.com/t/finding-methods-of-instance-itself/98943>\
**Category:** Ruby\
**Created:** [July 19, 2006, 5:58am UTC](https://www.ruby-forum.com/t/finding-methods-of-instance-itself/98943 "2006-07-19T05:58:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 19, 2006, 5:58am UTC](https://www.ruby-forum.com/t/finding-methods-of-instance-itself/98943/1 "2006-07-19T05:58:11Z")

</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:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 19, 2006, 6:26am UTC](https://www.ruby-forum.com/t/finding-methods-of-instance-itself/98943/2 "2006-07-19T06:26:22Z")

</div>

removed\_email\_address@domain.invalid wrote:

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

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 19, 2006, 6:48am UTC](https://www.ruby-forum.com/t/finding-methods-of-instance-itself/98943/3 "2006-07-19T06:48:06Z")

</div>

removed\_email\_address@domain.invalid wrote:

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

Thanks!

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 19, 2006, 9:45am UTC](https://www.ruby-forum.com/t/finding-methods-of-instance-itself/98943/4 "2006-07-19T09:45:58Z")

</div>

removed\_email\_address@domain.invalid schrieb:

> 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

---

<div class="post-metadata">

**Author:** ![lstrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e68b1a/32.png) [@lstrecv](https://www.ruby-forum.com/u/lstrecv)\
**Post date:** [July 19, 2006, 10:14am UTC](https://www.ruby-forum.com/t/finding-methods-of-instance-itself/98943/5 "2006-07-19T10:14:26Z")

</div>

On 19/07/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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