Skip to content

Instantly share code, notes, and snippets.

@stoffie
Created November 19, 2015 19:58
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 stoffie/d9dc3551f8558e482336 to your computer and use it in GitHub Desktop.
Save stoffie/d9dc3551f8558e482336 to your computer and use it in GitHub Desktop.
array = ["america", "europe", "asia", "africa", "france", "usa", "spain", "paris", "los angeles"]
definitions = {
"continent" => ["america", "europe", "asia", "africa"],
"country" => ["france", "usa", "spain"],
"city" => ["paris", "los angeles"],
"planet" => ["mars","earth"]
}
inverse_array = definitions.map {|k,v| v.map { |e| [e, k]}}.flatten(1)
inverse_hash = Hash[inverse_array]
output = array.map { |e| inverse_hash[e] }
puts output.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment