Skip to content

Instantly share code, notes, and snippets.

@ilake
Last active December 19, 2015 17:28
Show Gist options
  • Save ilake/5990853 to your computer and use it in GitHub Desktop.
Save ilake/5990853 to your computer and use it in GitHub Desktop.

SimpleDelegator

References

Note

  • 讓自己的class 可以利用include, inherit 得到其他module, class 的能力, 除此之外還可以利用delegate
 class A < SimpleDelegator
 end

 class B
   def do_something
     puts "B has done something"
   end
 end

 obj = A.new(B.new)

 obj.do_something
  • 大部分時候還是用include module 的方式比較好, 這樣在debug 時也比較容易去找, 因為delegator 會因為傳入的object 的不同, 呼叫的method 可能會分散在各個不同的file 裡增加 debug 的困難
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment