Reputation: 113
I am looking into catching every method that is defined on a base class, looking up what file it is defined in, and then doing some logic based on that.
I currently have:
# Defined in some file
class Subclass < Base
def foo
end
end
class Base
self.method_added(method)
# self is a given subclass (Subclass)
# This doesn't work. :(
self.method(method).source_location
end
end
What I'd like to be able to do is find out the source location of that method.
I could do something like:
self.new.method(source).source_location
But don't think I should be having to instantiate anything to get this to work.
Any ideas?
Upvotes: 4
Views: 845
Reputation: 13739
You can use method Module#instance_method to get instance method of your class:
instance_method(method).source_location # `self` is unnecessary, it is added implicitly
# => ["/home/alex/Projects/test/test.rb", 23]
instance_method(symbol) → unbound_method
Returns an UnboundMethod representing the given instance method in mod.
Upvotes: 4