Created
October 14, 2008 20:08
-
-
Save automatthew/16782 to your computer and use it in GitHub Desktop.
Distinguish origin of Ruby methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# "local" methods are those defined directly on a constant, | |
# as opposed to those inherited or mixed in | |
class Module | |
def local_class_methods | |
self.methods.select { |m| self.method(m).owner == self } | |
end | |
def local_instance_methods | |
self.instance_methods.select { |m| self.instance_method(m).owner == self } | |
end | |
end | |
# 1.8.7 has the owner method already, so just hack one up for 1.8.6 | |
class UnboundMethod | |
unless defined?(owner) | |
def owner | |
x = self.to_s.match( /: ([\w_:]+)(?:\(([\w_:]+)\))?/ ) | |
eval( x[2] || x[1] ) | |
end | |
end | |
end | |
class Method | |
unless defined?(owner) | |
def owner | |
x = self.to_s.match( /: ([\w_:]+)(?:\(([\w_:]+)\))?/ ) | |
eval( x[2] || x[1] ) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment