Skip to content

Instantly share code, notes, and snippets.

@jneen
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jneen/dd43070d5d2b9afba2a0 to your computer and use it in GitHub Desktop.
Save jneen/dd43070d5d2b9afba2a0 to your computer and use it in GitHub Desktop.
module Kernel
def cases(o, cases={})
o = [o] unless o.is_a? Array
raise "invalid" unless o[0].is_a? Keyword
selection = cases[o[0]] || cases[:else] || raise("non-exhaustive cases!")
selection.call(*cases[1..-1])
end
end
order = [:delivery, "123 Main St."]
order = [:digital, "customer@example.com"]
order = [:pickup, 456]
cases(order,
delivery: ->(addr) { ... }
digital: ->(email) { ... }
pickup: ->(store_id) { ... }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment