I'm trying to add different values into an array for the same key into the hash. Instead of making a new instance in the array, my function sum ups the index values of the array elements
def dupe_indices(array)
hash = Hash.new([])
array.each.with_index { |ele, idx| hash[ele] = (idx) }
hash
end
I'm getting this
print dupe_indices(['a', 'b', 'c', 'a', 'c']) => {"a"=>3, "b"=>1,
"c"=>4}
Expected output
print dupe_indices(['a', 'b', 'c', 'a', 'c']) => { 'a' => [0, 3], 'b'
=> [1], 'c' => [2, 4] }
ary.each_index.group_by { |i| ary[i] }