Last active
August 29, 2015 14:10
-
-
Save jneen/dd43070d5d2b9afba2a0 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
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