清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
class Object def self.overridden_methods(parent_class = Object, within_tree = true) if within_tree defined_methods = ancestors[0..ancestors.index(parent_class) - 1].map { |object| object.instance_methods(false) }.flatten.uniq parent_methods = superclass.instance_methods else defined_methods = instance_methods(false) parent_methods = parent_class.instance_methods end defined_methods & parent_methods end def overridden_methods(parent_class = Object, within_tree = true) self.class.overridden_methods(parent_class, within_tree) end def self.overrides?(method, parent_class = Object, within_tree = true) overridden_methods(parent_class, within_tree).include?(method.to_s) end def overrides?(method, parent_class = Object, within_tree = true) self.class.overrides?(method, parent_class, within_tree) end end class ParentModel < Object def save; end end class ChildModel < ParentModel def save; end def create; end def to_s; end end class AnotherChildModel < ParentModel def save; end end class GrandChildModel < ChildModel def create; end end # what have you overridden from the base `Object` class p ParentModel.overridden_methods(Object) #=> [] p ParentModel.overridden_methods(Object, false) #=> [] p ChildModel.overridden_methods(Object) #=> ["to_s", "save"] p ChildModel.overridden_methods(Object, false) #=> ["to_s"] p ChildModel.overridden_methods(ParentModel) #=> ["to_s", "save"] p ChildModel.overridden_methods(ParentModel, false) #=> ["to_s", "save"] p AnotherChildModel.overridden_methods(Object) #=> ["save"] p AnotherChildModel.overridden_methods(Object, false) #=> [] p AnotherChildModel.overridden_methods(ParentModel) #=> ["save"] p AnotherChildModel.overridden_methods(ParentModel, false) #=> ["save"] p GrandChildModel.overridden_methods(Object) #=> ["create", "to_s", "save"] p GrandChildModel.overridden_methods(Object, false) #=> [] p GrandChildModel.overridden_methods(ParentModel) #=> ["create", "to_s", "save"] p GrandChildModel.overridden_methods(ParentModel, false) #=> [] p GrandChildModel.overridden_methods(ChildModel) #=> ["create"] p GrandChildModel.overridden_methods(ChildModel, false) #=> ["create"]