Guest User

Untitled

a guest
Jul 17th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1.  
  2.  
  3. In the last case where you opened up the classs. Poor form? What
  4. about the ability to reflect on how a class is extended? For instance
  5. ActiveRecord::Base.included_modules and/or ActiveRecord::Base.extended_by.
  6. Opening up classes looks the ability for reflection to how it was changed.
  7.  
  8. Here is a snippet of code below to expand on my point. I just found out
  9. that the class method extended_by comes from ActiveSupport (I think) but
  10. it's not vanilla ruby. So maybe this is where the pattern evolved. IE,
  11. by using included and letting include do the extend, we are able to reflect
  12. on how us/others have extended the class. Which is funny, because the
  13. english of it should be allowed. Hence is there a default Ruby way to
  14. reflect on extended?
  15.  
  16. All of what your saying might make sense for one’s own lib and code
  17. organization. Like for instance, the author of ActiveRecord might
  18. just follow your simple pattern at the top and by the time I load up
  19. ActiveRecord::Base and asks for it’s included modules, I get a clean
  20. slate of [Kernel] which makes the default idiom (which yes does not
  21. match english) work. Thoughts?
  22.  
  23.  
  24.  
  25. class SomebodyElsesLib
  26.  
  27. end
  28.  
  29. module MetaSkills
  30. module KillerFeature
  31.  
  32. def self.inlcuded(klass)
  33. klass.class_eval do
  34. extend ClassMethods
  35. include InstanceMethods
  36. end
  37. end
  38.  
  39. module InstanceMethods
  40.  
  41. def killa_instance
  42. 'instance'
  43. end
  44.  
  45. end
  46.  
  47. module ClassMethods
  48.  
  49. def acts_as_killa
  50. 'class'
  51. end
  52.  
  53. end
  54.  
  55. end
  56. end
  57.  
  58. SomebodyElsesLib.send :include, MetaSkills::KillerFeature
  59.  
  60. SomebodyElsesLib.included_modules # => [MetaSkills::KillerFeature, Kernel]
  61. SomebodyElsesLib.extended_by # ~> -:40: undefined method `extended_by' for SomebodyElsesLib:Class (NoMethodError)
Add Comment
Please, Sign In to add comment