Skip to content

Instantly share code, notes, and snippets.

@automatthew
Created October 14, 2008 20:08
Show Gist options
  • Save automatthew/16782 to your computer and use it in GitHub Desktop.
Save automatthew/16782 to your computer and use it in GitHub Desktop.
Distinguish origin of Ruby methods
# "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