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