Simplified version:
ar=['name: Joe', 'name: Jack', 'name: Jill']
hash={}
ar.each{|line| if line.include?('name'); hash['name'] = line;end;}
puts hash
My aim is to add all the elements in the array to the ‘name’-key, but my code seeme to over-write the existing value.
Is’nt it possible to have multiple values assigned to one single key? I’d rather not use an array as value.
Edit:
I realized that my first example did’nt cover the whole problem:
extend the array to:
ar=[‘name: Joe’, ‘name: Jack’, ‘name: Jill’, age: 29′, ‘age: 32’, ‘misc: Great weather’]
What i’d is to have the ‘name’s grouped under the key ‘name’, and so on.
The number of attributes/keys is not known beforehand, so the arrays will have top be made dynamiclly.
There’s no option besides using an array as a value. Otherwise how would you retrieve the multiple values? You’d need some kind of enumerable anyway, so there’s no problem with using an array.
Try something like this:
Or, a little more simply:
For your edited question: