Sha256: 0e386ec5254076f13efb18add9ae32f62313c89f7a87fb2a95b08b39ddb8bb2d

Contents?: true

Size: 713 Bytes

Versions: 147

Compression:

Stored size: 713 Bytes

Contents

# frozen_string_literal: true

class Object
  # A duck-type assistant method. For example, Active Support extends Date
  # to define an <tt>acts_like_date?</tt> method, and extends Time to define
  # <tt>acts_like_time?</tt>. As a result, we can do <tt>x.acts_like?(:time)</tt> and
  # <tt>x.acts_like?(:date)</tt> to do duck-type-safe comparisons, since classes that
  # we want to act like Time simply need to define an <tt>acts_like_time?</tt> method.
  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
end

Version data entries

147 entries across 138 versions & 16 rubygems

Version Path
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/core_ext/object/acts_like.rb
activesupport-7.0.0 lib/active_support/core_ext/object/acts_like.rb
activesupport-6.1.4.4 lib/active_support/core_ext/object/acts_like.rb
activesupport-6.0.4.4 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.0.0.rc3 lib/active_support/core_ext/object/acts_like.rb
activesupport-6.1.4.3 lib/active_support/core_ext/object/acts_like.rb
activesupport-6.0.4.3 lib/active_support/core_ext/object/acts_like.rb
activesupport-6.0.4.2 lib/active_support/core_ext/object/acts_like.rb
activesupport-6.1.4.2 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.0.0.rc2 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.0.0.rc1 lib/active_support/core_ext/object/acts_like.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/core_ext/object/acts_like.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/core_ext/object/acts_like.rb
activesupport-7.0.0.alpha2 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.0.0.alpha1 lib/active_support/core_ext/object/acts_like.rb
activesupport-6.1.4.1 lib/active_support/core_ext/object/acts_like.rb
activesupport-6.0.4.1 lib/active_support/core_ext/object/acts_like.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4/lib/active_support/core_ext/object/acts_like.rb
mumukit-content-type-1.11.1 vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.4/lib/active_support/core_ext/object/acts_like.rb
activesupport-6.1.4 lib/active_support/core_ext/object/acts_like.rb