RCR: instance_methods(ancestor)

T

Trans

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

James Coglan

[Note: parts of this message were removed to make it a legal post.]
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top