For ease of authoring I'm writing my hash like this:
h = {
:key1: [:val1, :val2, :val3],
:key2: [:val4, :val5, :val6]
}
But everywhere I use it I need to look up the key associated with a value. Currently I'm doing the following to transform it:
h = Hash[*{
:key1: [:val1, :val2, :val3],
:key2: [:val4, :val5, :val6]
}.map {|key, vals| vals.map {|val| [val, key]}}.flatten]
Which gives me what I want:
{ :val1 => :key1, :val2 => key1, :val3 => key1, :val4 => key2, :val5 => :key2, :val6 => :key2 }
But is there a simpler way to achieve the same goal?