Return Array of Overridden Methods in Ruby Subclasses


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # thanks to the answer here:
  2. # http://stackoverflow.com/questions/3488203/how-do-i-see-where-in-the-class-hierarchy-a-method-was-defined-and-overridden-in
  3. class Object
  4. def self.overridden_methods(parent_class = Object, within_tree = true)
  5. if within_tree
  6. defined_methods = ancestors[0..ancestors.index(parent_class) - 1].map { |object| object.instance_methods(false) }.flatten.uniq
  7. parent_methods = superclass.instance_methods
  8. else
  9. defined_methods = instance_methods(false)
  10. parent_methods = parent_class.instance_methods
  11. end
  12. defined_methods & parent_methods
  13. end
  14.  
  15. def overridden_methods(parent_class = Object, within_tree = true)
  16. self.class.overridden_methods(parent_class, within_tree)
  17. end
  18.  
  19. def self.overrides?(method, parent_class = Object, within_tree = true)
  20. overridden_methods(parent_class, within_tree).include?(method.to_s)
  21. end
  22.  
  23. def overrides?(method, parent_class = Object, within_tree = true)
  24. self.class.overrides?(method, parent_class, within_tree)
  25. end
  26. end
  27.  
  28. class ParentModel < Object
  29. def save; end
  30. end
  31.  
  32. class ChildModel < ParentModel
  33. def save; end
  34. def create; end
  35. def to_s; end
  36. end
  37.  
  38. class AnotherChildModel < ParentModel
  39. def save; end
  40. end
  41.  
  42. class GrandChildModel < ChildModel
  43. def create; end
  44. end
  45.  
  46. # what have you overridden from the base `Object` class
  47. p ParentModel.overridden_methods(Object)
  48. #=> []
  49. p ParentModel.overridden_methods(Object, false)
  50. #=> []
  51. p ChildModel.overridden_methods(Object)
  52. #=> ["to_s", "save"]
  53. p ChildModel.overridden_methods(Object, false)
  54. #=> ["to_s"]
  55. p ChildModel.overridden_methods(ParentModel)
  56. #=> ["to_s", "save"]
  57. p ChildModel.overridden_methods(ParentModel, false)
  58. #=> ["to_s", "save"]
  59. p AnotherChildModel.overridden_methods(Object)
  60. #=> ["save"]
  61. p AnotherChildModel.overridden_methods(Object, false)
  62. #=> []
  63. p AnotherChildModel.overridden_methods(ParentModel)
  64. #=> ["save"]
  65. p AnotherChildModel.overridden_methods(ParentModel, false)
  66. #=> ["save"]
  67. p GrandChildModel.overridden_methods(Object)
  68. #=> ["create", "to_s", "save"]
  69. p GrandChildModel.overridden_methods(Object, false)
  70. #=> []
  71. p GrandChildModel.overridden_methods(ParentModel)
  72. #=> ["create", "to_s", "save"]
  73. p GrandChildModel.overridden_methods(ParentModel, false)
  74. #=> []
  75. p GrandChildModel.overridden_methods(ChildModel)
  76. #=> ["create"]
  77. p GrandChildModel.overridden_methods(ChildModel, false)
  78. #=> ["create"]
  79.  
  80. # note, this probably won't work with modules overriding methods, it just needs to be tweaked a little more

URL: return-array-of-overridden-methods-in-ruby-subclasses

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.