간격이라는 배열 배열이 있습니다. 각 해시 (start_ts 및 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
최종 결과는 7 가지 해시의 배열이어야합니다. 현재 어레이 totalarray는 추가되는 것과는 반대로 각 패스에서 덮어 쓰여진 것처럼 보입니다.
내가 뭘 잘못하고있어. 감사.
이 코드를 어떻게 실행 했습니까? 구문 오류가 너무 많습니다. –
@ArupRakshit 구문 오류가 하나 있습니다.'intervals.each do | int | do (두 번 두 번). –
나는 '날짜'를 요구하고 불쾌한 두 번째를 제거했습니다. 미안합니다. – user3707736