Created
June 5, 2009 07:37
-
-
Save joshsusser/124130 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
# an object representing a duck type that can be used in a case expression | |
class Protocol | |
def initialize(*selectors) | |
@selectors = selectors | |
end | |
def ===(object) | |
@selectors.all? {|selector| object.respond_to?(selector)} | |
end | |
end | |
rest = Protocol.new(:get, :post, :put, :delete) | |
sql = Protocol.new(:select, :insert, :update, :delete) | |
case obj | |
when rest | |
obj.get(things) | |
when sql | |
obj.select(things) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment