-
-
Save duckinator/8470407 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# In response to: | |
# http://mlomnicki.com/ruby/tricks-and-quirks/2011/02/10/ruby-tricks2.html | |
# Ruby 1.9.2 has some neat stuff that lets us make a readable | |
# alternative case statement that calls each method in turn. | |
# 1.9.2 features used: | |
# * hashes are ordered in 1.9.2 | |
# * cool JSON-style hash syntax | |
# * concise lambda syntax | |
module Kernel | |
def switch(thing, hash) | |
hash.each do |method, proc| | |
return proc[] if method == :else || thing.send(method) | |
end | |
end | |
end | |
switch offer, | |
available: -> { order.submit }, | |
cancelled: -> { order.reject }, | |
postponed: -> { order.postpone }, | |
else: -> { raise UnknownStateError.new } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment