Guest User

Untitled

a guest
Apr 24th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. class Object
  2. def chain_method(with_symbol, &new_method)
  3. old_method = method with_symbol
  4. eigenclass = class << self; self; end
  5. eigenclass.class_eval do
  6. define_method with_symbol do |*args| new_method[old_method, *args] end
  7. end
  8. end
  9. end
  10.  
  11. def foo(arg)
  12. print 'in foo: ', arg, "\n"
  13. end
  14.  
  15. puts
  16. foo 'first call'
  17.  
  18. chain_method :foo do |old_method, arg |
  19. puts 'in first block_eval'
  20. old_method.call arg
  21. end
  22.  
  23. puts
  24. foo 'second call'
  25.  
  26. chain_method :foo do |old_method, arg|
  27. puts 'in second block_eval'
  28. old_method.call arg
  29. end
  30.  
  31. puts
  32. foo 'third call'
Add Comment
Please, Sign In to add comment