【发布时间】:2017-08-07 17:57:44
【问题描述】:
我将这些哈希值作为输出,我需要将其推入数组而不被覆盖。
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
【问题讨论】:
-
请显示一些代码(例如,您“循环遍历”的位置)。
-
我认为它需要更多的代码。
i在哪里定义?entry_hash是什么?output是什么?attributes是什么?
标签: ruby-on-rails ruby ruby-on-rails-4