Created
February 18, 2014 10:48
-
-
Save SamSaffron/9068531 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
@@acts_like_cache = {} | |
# 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) | |
respond_to? (@@acts_like_cache[duck] ||= :"acts_like_#{duck}?") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment