我正在尝试将元素添加到哈希值。例子:
tally={}
tally["aa"]=[1]
tally["aa"].append(2) => so it should be tally["aa"]=[1,2]
我的代码:
def list_duplicates(seq)
tally = {}
seq.each_with_index do |item,i|
if tally[item].nil?
tally[item]=[i]
else
tally[item].add(i)
end
end
a=tally.keys()
b=tally.values()
if b.length>1
return [a,b]
end
end
sourse=["AAA","GGG","AAA","BBB","AAA","BBB","agaha"]
list_duplicates(sourse).each do |l|
puts(l)
end
但它向我显示了一个错误:
undefined method `append' for []:Array (NoMethodError)
如何将项目附加到现有项目的值?