Method: Object#acts_like?
- Defined in:
- lib/active_support/core_ext/object/acts_like.rb
#acts_like?(duck) ⇒ Boolean
A duck-type assistant method. For example, Active Support extends Date to define an acts_like_date? method, and extends Time to define acts_like_time?. As a result, we can do x.acts_like?(:time) and x.acts_like?(:date) to do duck-type-safe comparisons, since classes that we want to act like Time simply need to define an acts_like_time? method.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/active_support/core_ext/object/acts_like.rb', line 9 def acts_like?(duck) case duck when :time respond_to? :acts_like_time? when :date respond_to? :acts_like_date? when :string respond_to? :acts_like_string? else respond_to? :"acts_like_#{duck}?" end end |