Reputation: 105
I have an attributes array which is defined by me and I get an hash both of them which I need to combine into an output array. Please let me know the simplest way to do this.
attributes = [:user_id, :project_id, :task_id, :date, :time_spent, :comment]
entry_hash = {"User"=>1, "Project"=>[8], "Task"=>[87], "Date"=>"05/22/2017", "Time (Hours)"=>"1", "Comment"=>"yes"}
When it is combined I want a hash like
Output = {"user_id"=>1, "project_id"=>8, "task_id"=>87, "date"=>6/22/2017,"time_spent"=>1,"comment"=>"yes"}
Thanks for the help!
Upvotes: 0
Views: 41
Reputation: 1164
Try this
attributes = [:user_id, :project_id, :task_id, :date, :time_spent, :comment]
# puts attributes.inspect
entry_hash = {"User"=>1, "Project"=>[8], "Task"=>[87], "Date"=>"05/22/2017", "Time (Hours)"=>"1", "Comment"=>"yes"}
# puts entry_hash.inspect
output = {}
a = 0
entry_hash.each do |key,value|
if value.class == Array
output[attributes[a]] = value.first.to_s
#output[attributes[a]] = value.first.to_i //also you can convert them into int
else
output[attributes[a]] = value
end
a += 1
end
#puts output.inspect
#{:user_id=>1, :project_id=>"8", :task_id=>"87", :date=>"05/22/2017", :time_spent=>"1", :comment=>"yes"}
Upvotes: 1