Guest User

Untitled

a guest
Sep 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. class Proxy
  2. def initialize(obj, modules)
  3. @obj = obj
  4.  
  5. modules.each do |mod|
  6. extend mod
  7. end
  8. end
  9.  
  10. def method_missing(meth, *args, &block)
  11. @obj.send(meth, *args, &block).tap do |ret|
  12. return self if ret == @obj
  13. end
  14. end
  15. end
  16.  
  17. class Object
  18. def with_aspects(*modules)
  19. proxy = Proxy.new(self, modules)
  20. end
  21.  
  22. alias with_aspect with_aspects
  23. end
  24.  
  25. class Person
  26. def initialize(name)
  27. @name = name
  28. end
  29.  
  30. def hello
  31. "#{@name} says hello"
  32. end
  33.  
  34. def myself
  35. self
  36. end
  37. end
  38.  
  39. module Yeller
  40. def hello
  41. super.upcase
  42. end
  43. end
  44.  
  45. yehuda = Person.new("Yehuda")
  46.  
  47. p yehuda.with_aspect(Yeller).hello
  48. p yehuda.with_aspect(Yeller).myself.hello
Add Comment
Please, Sign In to add comment