I’m fairly new to Ruby/Rails, and I’m trying to figure out how to split a {"key" => ["val1", "val2"]} hash into a {"key" => "val1", "key" => "val2"} hash. I feel like I should flatten the hash and somehow build a new one up, but I’m unsure how to approach the problem. Thanks!
EDIT: Haha, shows how blinded I was by the trees to not see the forest. Can’t believe I made such a silly mistake. Thanks to everyone who shook me awake.
A Hash by definition cannot have the same key present more than once. Would you instead like an Array of arrays?
If so, and if every hash key is an array of values, then you can do this:
Note that
flatten(1)is Ruby 1.8.7+ onlyEdit: Per Nakilon’s comment below, this can be a hair simpler in Ruby 1.9.2+:
Edit: Or per @tokland’s comment below, even shorter/better using
Array#product: