Created
June 6, 2012 15:52
-
-
Save jordelver/2882852 to your computer and use it in GitHub Desktop.
Lookup Tables With Lambdas
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
# Adapted from Lookup Tables With Lambdas | |
# http://www.naildrivin5.com/blog/2012/05/16/lookup-tables-with-lambdas.html | |
# Lookup with lambdas so we can do a database call or something | |
CARD_TYPE_COUNTRIES = { | |
'discover' => lambda { ['US'] }, | |
'maestro' => lambda { ['UK'] }, | |
'amex' => lambda { Array.new(1, 'NOWHERE!') }, # Hit database | |
'default' => lambda { ['US','UK','ZA'] } | |
} | |
# Try to find the key, if not use the 'default' key | |
def countries_usable_in(card_type) | |
CARD_TYPE_COUNTRIES.fetch(card_type) do | |
CARD_TYPE_COUNTRIES['default'] | |
end.call | |
end | |
# Output | |
> countries_usable_in('discover') | |
=> ["US"] | |
> countries_usable_in('amex') | |
=> ["NOWHERE!"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment