Sha256: 66cf36b6fd44ccd0fb8423c4840a1cc9aac00421fae008184df0a677b892924f

Contents?: true

Size: 1.5 KB

Versions: 84

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

class Object
  # Provides a way to check whether some class acts like some other class based on the existence of
  # an appropriately-named marker method.
  #
  # A class that provides the same interface as <tt>SomeClass</tt> may define a marker method named
  # <tt>acts_like_some_class?</tt> to signal its compatibility to callers of
  # <tt>acts_like?(:some_class)</tt>.
  #
  # For example, Active Support extends <tt>Date</tt> to define an <tt>acts_like_date?</tt> method,
  # and extends <tt>Time</tt> to define <tt>acts_like_time?</tt>. As a result, developers can call
  # <tt>x.acts_like?(:time)</tt> and <tt>x.acts_like?(:date)</tt> to test duck-type compatibility,
  # and classes that are able to act like <tt>Time</tt> can also define an <tt>acts_like_time?</tt>
  # method to interoperate.
  #
  # Note that the marker method is only expected to exist. It isn't called, so its body or return
  # value are irrelevant.
  #
  # ==== Example: A class that provides the same interface as <tt>String</tt>
  #
  # This class may define:
  #
  #   class Stringish
  #     def acts_like_string?
  #     end
  #   end
  #
  # Then client code can query for duck-type-safeness this way:
  #
  #   Stringish.new.acts_like?(:string) # => true
  #
  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

84 entries across 80 versions & 12 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3.4/lib/active_support/core_ext/object/acts_like.rb
activesupport-8.0.1 lib/active_support/core_ext/object/acts_like.rb
activesupport-8.0.0.1 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.2.2.1 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.1.5.1 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.0.8.7 lib/active_support/core_ext/object/acts_like.rb
activesupport-8.0.0 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.2.2 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.1.5 lib/active_support/core_ext/object/acts_like.rb
activesupport-8.0.0.rc2 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.2.1.2 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.1.4.2 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.0.8.6 lib/active_support/core_ext/object/acts_like.rb
activesupport-8.0.0.rc1 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.2.1.1 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.1.4.1 lib/active_support/core_ext/object/acts_like.rb
activesupport-7.0.8.5 lib/active_support/core_ext/object/acts_like.rb
activesupport-8.0.0.beta1 lib/active_support/core_ext/object/acts_like.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/core_ext/object/acts_like.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/core_ext/object/acts_like.rb