I have the array tags, which consists of a number of Strings that I need to use to create a new Hash (content) where each value is an empty array. I currently have Hash[*tags.map {|k| [k, nil]}.flatten]
, but this returns:
{
"tag1" => nil,
"tag2" => nil,
"tag3" => nil
}
when I want it to be
{
"tag1" => [],
"tag2" => [],
"tag3" => []
}
sorry this is kind of a dumb question, but I've googled around and can't find the answer. Thank you!
Using flatten, map
[]
instead ofnil
like you tried, then useflatten(1)
. That eliminates only the first layer of array, so you get['tag1', [], ...]
to pass toHash[]
.You can also avoid
flatten
altogether if you drop the splat (*
) fromHash[]
, since ::[] also accepts a list of pairs.