我有這些散列作爲輸出,我需要推入數組而不覆蓋。將哈希推入數組
output1= {:user_id=>9, :project_id=>4, :task_id=>87, :comment=>"Test 20"}
output2 = {:user_id=>9, :project_id=>12, :task_id=>105,:comment=>"Test 21"}
我需要推動這2輸出到一個單一的數組,當我遍歷循環。現在發生的事情是,當我將第二個輸出插入到數組中時,它會覆蓋第一個輸出,下面是我得到的結果。
Entry_array=[{:user_id=>9,:project_id=>12,:task_id=>105,:comment=>"Test 21"},
{:user_id=>9, :project_id=>12, :task_id=>105,:comment=>"Test 21"}]
我想要結合散列輸出1和散列輸出2的結果。 非常感謝所有的幫助。
這是我使用
attributes =[:user_id,:project_id,task_id,:comment]
entry_array=[]
output = {}
CSV.foreach(csv_file, headers: true, converters: :date).with_index do |row,line_no|
entry_hash= row.to_hash
.....some code here where we get the entry_hash....
i=0
entry_array <<output
end
請顯示一些代碼(例如,在'循環遍歷'的位置)。 – jvillian
我認爲它需要更多一點的代碼。「我」定義在哪裏?什麼是'entry_hash'?什麼是「輸出」?什麼是「屬性」? –