Reputation: 79488
Is there a way to know whether or not a method has been overridden by a subclass programmatically? Something that works like this:
class BaseModel
def create
puts "superclass"
end
end
class SomeModel < BaseModel
def create
puts "subclass"
end
end
puts SomeModel.overridden_instance_methods #=> [:create]
Any ideas?
Upvotes: 2
Views: 100
Reputation: 370465
SomeModel.instance_methods(false) & BaseModel.instance_methods
The false
makes instance_methods
not include inherited methods. We then use set intersection to find all the methods that were defined on SomeModel which have previously been defined on BaseModel (or Object).
Upvotes: 4