-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"}
1
  • 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? Commented Feb 14, 2017 at 19:49

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.