RubyTapas #016 - #020
016 super in Modules
★
defined?(super) を使う
module YeOlde
def hello(subject="World")
if defined?(super)
super
else
あるメソッド(sendとか)を上書きするmoduleがあった場合
original_send = Object.instance_method(:send)
bound_send = original_send.bind(self)
bound_send.call(name, *args, &block)
なんか最近これっぽいの見た
rails 3.2.13のactive supportで動かなくなってた
017 Pay it Forward
★
Command method
Shell.execute()
stub (Query method)
Shell.stub(:execute, )
後半重要そうだけどよくわからなかった
018 Subclassing Array
★★
ArrayのsubclassのTagList作ったけど、処理を重ねるうちにいつのまにかArrayになっていた!
内部でArrayを持って、いくつかのメソッドを実装すれば良い
eachも重要
019 Pluggable Selector
★
SleepTimer.new(minutes, $stdout, :puts)
ProductListPresenter.new(products, :short_name).render
ProductListPresenter.new(products, :long_name).render
のようにできる
セキュリティのためsendよりも
notifier.public_send(notify_message, "Tea is ready!")
の方が良い
020 Struct
★
enumerable
each, each_pair
Struct.new ... do
Railsばかりだとあまり必要と感じたケースはない