RubyTapas #091 - #093
091 Ruby 2.0: Rebinding Methods
★
DCI - Data, Context and Interaction
Roles
source_account.extend(TransferSourceAccount)
dest_account.extend(TransferDestinationAccount)
Wrap
source_account = TransferSourceAccount.new(account1)
dest_account = TransferDestinationAccount.new(account2)
Ruby 2.0
def method_missing(method_name, *args, &block)
if role && role.public_method_defined?(method_name)
role.instance_method(method_name).bind(self).call(*args, &block)
source_account.play_role(TransferSourceAccount) do
dest_account.play_role(TransferDestinationAccount) do
source_account.transfer_to(dest_account, 25_00)
難しい
092 Coincidental Duplication Redux
★
over-DRYing code
suffering from reduced readability as a result of unfamiliar, over-genericized terminology
DRYで短ければ必ずしも読みやすいとは限らない
違うConceptのものを無理矢理まとめようとすると読みにくくなることもある
093 Boolean
★
((true.methods & false.methods) - Object.instance_methods)
# => [:&, :|, :^]
caseを使いたいケース
when true, false
ugly
if value == true || value == false