Skip to content

Instantly share code, notes, and snippets.

@jordelver
Created June 6, 2012 15:52
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 jordelver/2882852 to your computer and use it in GitHub Desktop.
Save jordelver/2882852 to your computer and use it in GitHub Desktop.
Lookup Tables With Lambdas
# 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