概要
Object#acts_like?
詳細
Object#acts_like?について
Object#acts_like?
acts_like_xxx?を実装している場合に
acts_like? :xxxを呼び出すと真を返却する。
ダックタイピングを利用する際のマーカーとして利用する。
サンプル
# encoding: utf-8 require "active_support/core_ext/object/acts_like" require "tbpgr_utils" class Duck def acts_like_duck? end def quack "quack!!quack!!" end end class ImitateDuckPerson def acts_like_duck? end def quack "がー!!がー!!" end end class Person def hello "こんちわ" end end [Duck.new, ImitateDuckPerson.new, Person.new].each do |duck| next unless duck.acts_like? :duck puts_eval "duck.quack", binding end
出力
duck.quack # => "quack!!quack!!" duck.quack # => "がー!!がー!!"