-1

I want to use each item in the array as values in a hash with the same key "name".

people = ["Bob", "Mary", "Sarah", "Tim", "Maggie"]

I want to get:

{ name: => "Bob", name: => "Mary", name: => "Sarah", name: => "Tim", name: => "Maggie"}

When I do Hash[people.map {|v| ["name", v]}] or

people.map{|v| hash['name'] = v}

it zips only the last item so I get this as a result :

{"name"=>"Maggie"}
  • 2
    What do you expect `hash[:name]` to return, in the hypothetical world where it were possible to have a key map to multiple values? – user229044 Feb 14 '17 at 19:49

1 Answers1

2

This is impossible. Keys in a hash cannot be duplicated. Each key may only exist once, and map to exactly one value.

user229044
  • 232,980
  • 40
  • 330
  • 338