fork download
  1. #
  2. # http://q...content-available-to-author-only...e.jp/1370659386
  3. #
  4.  
  5. module Foo
  6. def initialize
  7. @original_update = self.method(:update)
  8. def self.update
  9. @original_update.call
  10. puts "!!! Extended Implementation !!!"
  11. end
  12. end
  13. end
  14.  
  15. class Bar
  16. def update
  17. puts "Bar : update"
  18. end
  19. end
  20.  
  21. class Baz < Bar
  22. def update
  23. super
  24. puts "Baz : update"
  25. end
  26. end
  27.  
  28.  
  29. #
  30. # Test
  31. #
  32. def test msg
  33. puts "\n--------------------------------\n#{msg}\n--------------------------------"
  34. puts "-- Bar.update"
  35. f = Bar.new
  36. f.update
  37.  
  38. puts "-- Baz.update"
  39. f = Baz.new
  40. f.update
  41.  
  42. end
  43.  
  44. test "Normal"
  45.  
  46. class Baz
  47. include Foo
  48. end
  49.  
  50. test "Baz include Foo"
  51.  
  52. class Bar
  53. include Foo
  54. end
  55.  
  56. test "Bar include Foo"
  57.  
Success #stdin #stdout 0.01s 7460KB
stdin
Standard input is empty
stdout
--------------------------------
Normal
--------------------------------
-- Bar.update
Bar : update
-- Baz.update
Bar : update
Baz : update

--------------------------------
Baz include Foo
--------------------------------
-- Bar.update
Bar : update
-- Baz.update
Bar : update
Baz : update
!!! Extended Implementation !!!

--------------------------------
Bar include Foo
--------------------------------
-- Bar.update
Bar : update
!!! Extended Implementation !!!
-- Baz.update
Bar : update
Baz : update
!!! Extended Implementation !!!