Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Sep 24th, 2018 65 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top