create date range array from a list of dates using ruby -
create date range array from a list of dates using ruby -
what best way create array of hashes date ranges list of dates. example: if have list of dates in array below:
['12/11/2014','13/11/2014','14/11/2014','24/11/2014','25/11/2014','26/11/2014','27/11/2014','28/11/2014','29/11/2014','04/12/2014','05/12/2014','06/12/2014','07/12/2014','24/12/2014','25/12/2014','26/12/2014', '28/12/2014', '30/12/2014']
i trying array of date ranges hashes below:
[{:from => '12/11/2014', :to => '14/11/2014'}, {:from => '24/11/2014', :to => '29/11/2014'}, {:from => '04/12/2014', :to => '07/12/2014'}, {:from => '24/12/2014', :to => '26/12/1014'}, {:from => '28/12/2014', :to => '28/12/2014'}, {:from => '30/12/2014', :to => '30/12/2014'}]
anything available in ruby generate ? thanks.
enumerable has slice_before
method can want, add-on of added code. the documentation:
if block needs maintain state on multiple elements, local variables can used. example, 3 or more consecutive increasing numbers can squashed follows:
a = [0, 2, 3, 4, 6, 7, 9] prev = a[0] p a.slice_before { |e| prev, prev2 = e, prev prev2 + 1 != e }.map { |es| es.length <= 2 ? es.join(",") : "#{es.first}-#{es.last}" }.join(",") #=> "0,2-4,6,7,9"
obviously isn't finish answer, not @ computer has code want.
ruby arrays date
Comments
Post a Comment