我如何比较:
中的相邻元素line = 'Aug 2013-Mar 2014; Apr 2013-Aug 2013; Jun 2014-Aug 2015; Apr 2003-Nov 2004; Apr 2014-Jan 2015'
检查是否:
如果条件满足,我想将后两个值从前一个元素移动到组合或硬设置值中的当前元素,并删除前一个元素。
#Expected result => [[2003, 4, 2004, 11], [2013, 4, 2015, 8]]
这是我的尝试:
require 'date'
line = line.split(/[-,;]/)
months = []
years = []
line.each do |x|
x = DateTime.parse(x)
months << x.mon
years << x.year
end
years.zip(months).flatten.each_slice(4).to_a.sort_by{|x| [x[2],x[3]]}
#=> [[2003, 4, 2004, 11], [2013, 4, 2013, 8], [2013, 8, 2014, 3], [2014, 4, 2015, 1], [2014, 6, 2015, 8]]