pgu*_*rio 28
例如:
((Date.today - 90)..Date.today).map{|d| [d.year, d.month]}.uniq
#=> [[2012, 12], [2013, 1], [2013, 2], [2013, 3]]
Run Code Online (Sandbox Code Playgroud)
我想出了这个解决方案来生成范围内所有[年,月]元组的列表:
first=[2012,10]
last=[2013,03]
(first[0]..last[0]).to_a.product((1..12).to_a).select{|ym|(first..last).cover?(ym)}
=> [[2012, 10], [2012, 11], [2012, 12], [2013, 1], [2013, 2], [2013, 3]]
Run Code Online (Sandbox Code Playgroud)