我想列出所有周数和年份。这就是我所拥有的:
start # 2012-05-10
ende # 2013-06-20
while start < ende
weeks << start.cweek
start += 1.week
end
列出所有周数:
@kws.each do |w|
w
end
我需要一些灵感,如何将相应的年份分配给每个周数。这样我就可以得到 22 / 2012 23 / 2012 等。
感谢帮助..
我想列出所有周数和年份。这就是我所拥有的:
start # 2012-05-10
ende # 2013-06-20
while start < ende
weeks << start.cweek
start += 1.week
end
列出所有周数:
@kws.each do |w|
w
end
我需要一些灵感,如何将相应的年份分配给每个周数。这样我就可以得到 22 / 2012 23 / 2012 等。
感谢帮助..
hash
用key
as ayear
和value
as an创建一个array of week numbers
start # 2012-05-10
ende # 2013-06-20
weeks ={}
while start < ende
weeks[start.year] = [] unless weeks[start.year]
weeks[start.year] << start.cweek
start += 1.week
end
p weeks
你得到 o/p
=> {2012=>[19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52],
2013=>[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24]}
在您的 while 循环中,您还可以存储年份,一个简单的方法就是作为数组数组。
然后在稍后的每个循环中,您可以访问两者:
start = Date.new( 2012, 5, 10 )
ende = Date.new( 2013, 6, 20 )
weeks = []
while start < ende
weeks << [start.cweek, start.year] # <-- enhanced
start += 1.week
end
weeks.each do |w,y| # <-- take two arguments in the block
puts "#{w} / #{y}" # and print them both out
end
结果:
=>
19 / 2012
20 / 2012
21 / 2012
22 / 2012
23 / 2012
24 / 2012
25 / 2012
...
22 / 2013
23 / 2013
24 / 2013