如何在 Ruby 中将年份数组转换为月份数组? [英] How to turn an array of years into an array of months in Ruby?
本文介绍了如何在 Ruby 中将年份数组转换为月份数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一系列年份:
[2013, 2012, 2011, 2010, 2009]
我怎样才能把它变成一个包含那些年的每个月的数组?
How can I turn this into an array containing each month of those years?
[2013-12, 2013-11, 2013-10, 2013-09...]
感谢您的帮助!
推荐答案
years = [2013, 2012, 2011, 2010, 2009]
years.flat_map { |year|
12.downto(1).map { |month|
sprintf("%04d-%02d", year, month) # OR "%04d-%02d" % [year, month]
}
}
# => ["2013-12", "2013-11", "2013-10", .., "2009-02", "2009-01"]
这篇关于如何在 Ruby 中将年份数组转换为月份数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文