Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.44 KB | None | 0 0
  1. class Module
  2.     def before_method method, &block
  3.         alias_method "__old__#{method.to_s}".to_sym, method unless respond_to? "__old__#{method.to_s}".to_sym
  4.         define_method method do |*args|
  5.             block.call(*args)
  6.             send "__old__#{method.to_s}".to_sym, *args
  7.         end
  8.     end
  9. end
  10.  
  11. class Test
  12.     def greet who
  13.         puts "hello #{who}"
  14.     end
  15. end
  16.  
  17. Test.before_method :greet do |w|
  18.     puts "Calling a greeting for #{w}"
  19. end
  20.  
  21. b = Test.new
  22. b.greet "world"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement