Skip to content

Instantly share code, notes, and snippets.

@joshsusser
Created June 5, 2009 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joshsusser/124130 to your computer and use it in GitHub Desktop.
Save joshsusser/124130 to your computer and use it in GitHub Desktop.
# 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