# RCR: instance\_methods(ancestor)

**URL:** <https://www.ruby-forum.com/t/rcr-instance-methods-ancestor/146727>\
**Category:** Ruby\
**Created:** [August 24, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/rcr-instance-methods-ancestor/146727 "2008-08-24T20:16:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 24, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/rcr-instance-methods-ancestor/146727/1 "2008-08-24T20:16:18Z")

</div>

= Problem

Currently Module#instance\_methods (and company) take a single  
argument, true or false, as to whether to include all ancestor  
methods. I’ve written enough metacode to come across the need to limit  
the search at a particular ancestor. This currently requires code  
like:

```
  meths = []
  ancestors[0..ancestors.index(FooAncestor)].each do |anc|
    meths = meths | anc.instance_methods(false)
  end

```

It would be nice if we could simply say:

instance\_methods(FooAncestor)

= Solution

I leave the full solution to more capable C peoples. I imagine it  
would not be too difficult however.

= Analysis

This change can be backward compatible. It also eliminates the dreaded  
true|false argument, and can be applied to the entire family of  
“methods” methods.

T.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 24, 2008, 8:22pm UTC](https://www.ruby-forum.com/t/rcr-instance-methods-ancestor/146727/2 "2008-08-24T20:22:44Z")

</div>

> It would be nice if we could simply say:
> 
> instance\_methods(FooAncestor)

You could monkey-patch, if you’re into that sort of thing. This  
maintains  
the existing method signature while adding your desired behaviour:

class Module  
method = instance\_method :instance\_methods  
define\_method :instance\_methods do |condition = true|  
return method.bind(self).call(condition) unless Module === condition  
anc = ancestors  
index = anc.index(condition) || -1  
anc[0…index].inject([]) { |methods, ancestor|  
methods.concat(ancestor.instance\_methods(false))  
}.uniq  
end  
end
