I have an array of arrays called intervals. I wish to build an array of hashes out of it, adding two key/value pairs to each hash (start_ts and stop_ts).
require 'date'
date = '2014-06-12'
totalhash = Hash.new
totalarray = Array.new
payload2 = Array.new
totals = Array.new
intervals = [["Current", 0, 9999],
["1 to 4", -4, -1],
["5 to 15", -15, -5],
["16 to 30", -30, -16],
["31 to 60", -60, -31],
["61 to 90", -90, -61],
["91+", -9999, -91]]
intervals.each do |int|
label, start, stop = int
# Parse date and then convert to UNIX epoch (.to_time.to_i chain)
start_ts = (Date.parse("#{date}") + start).to_time.to_i
stop_ts = (Date.parse("#{date}") + stop).to_time.to_i
totalhash[:label] = label
totalhash[:start] = start
totalhash[:stop] = stop
totalhash[:start_ts] = start_ts
totalhash[:stop_ts] = stop_ts
totalarray << totalhash
totals = totalarray.reduce Hash.new, :merge
puts totals
puts 'totals size: ' + totals.size.to_s
end
The end result should be an array of seven hashes. Currently the array totalarray seems to be overwritten on each pass as opposed to being appended to.
What am I doing wrong. Thanks.
intervals.each do |int| do(dotwice).totalsis intended to represent?