fork(1) download
  1. module A
  2. def doit
  3. puts 'A doit'
  4. end
  5. end
  6.  
  7. module B
  8. def doit
  9. puts 'B doit'
  10. end
  11. end
  12.  
  13. module C
  14. def self.included(base)
  15. base.prepend(InstanceMethods)
  16. end
  17.  
  18. module InstanceMethods
  19. def doit
  20. puts 'C doit'
  21. end
  22. end
  23. end
  24.  
  25. module D
  26. def self.included(base)
  27. base.class_eval do
  28. def doit
  29. puts 'D doit'
  30. end
  31. end
  32. end
  33. end
  34.  
  35. class Foo
  36. include A
  37. end
  38.  
  39. f = Foo.new
  40. puts Foo.ancestors.inspect
  41. f.doit
  42.  
  43. puts '---- B ----'
  44.  
  45. # Not working
  46. A.include(B)
  47. puts A.ancestors.inspect
  48. puts Foo.ancestors.inspect
  49. f.doit
  50.  
  51. puts '---- C ----'
  52.  
  53. # Not working
  54. A.include(C)
  55. puts A.ancestors.inspect
  56. puts Foo.ancestors.inspect
  57. f.doit
  58.  
  59. puts '---- D ----'
  60.  
  61. # Working
  62. A.include(D)
  63. puts A.ancestors.inspect
  64. puts Foo.ancestors.inspect
  65. f.doit
Success #stdin #stdout 0s 28224KB
stdin
Standard input is empty
stdout
[Foo, A, Object, Kernel, BasicObject]
A doit
---- B ----
[A, B]
[Foo, A, Object, Kernel, BasicObject]
A doit
---- C ----
[C::InstanceMethods, A, C, B]
[Foo, A, Object, Kernel, BasicObject]
A doit
---- D ----
[C::InstanceMethods, A, D, C, B]
[Foo, A, Object, Kernel, BasicObject]
D doit