Guest User

Untitled

a guest
Feb 20th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #--
  2. # George Moschovitis <gm@navel.gr>
  3. #++
  4.  
  5. class Module
  6.  
  7. # A useful macro for dynamic modules.
  8. #--
  9. # FIXME: quick and easy implementation, should
  10. # come up with something better. The name
  11. # sucks too.
  12. #++
  13. def on_included(code)
  14. tag = caller[0].split(' ').first.split(/\/|\\/).last.gsub(/:|\.|\(|\)/, '_')
  15. old = "__included_#{tag}"
  16. module_eval %{
  17. class << self
  18. alias_method :#{old}, :included
  19. def included(base)
  20. #{old}(base)
  21. #{code}
  22. end
  23. end
  24. }
  25. end
  26. end
  27.  
  28. =begin Testing
  29.  
  30. module M
  31. on_included %{
  32. puts 'hello'
  33. }
  34. end
  35.  
  36. module Q
  37. on_included %{
  38. puts 'world'
  39. }
  40. on_included %{
  41. puts 'it works'
  42. }
  43. end
  44.  
  45. class C
  46. include M
  47. include Q
  48. end
  49.  
  50. =end
Add Comment
Please, Sign In to add comment