連続した日付をまとめたい

2014-01-01,a
2014-01-02,b
2014-01-03,c
2014-01-04,d
2014-01-06,e
2014-01-07,f
2014-01-09,g
2014-01-11,h

こういう一列目に日付が入ったCSVデータがあるとして、日付が連続している期間をこんな感じでまとめたい。

2014-01-01 - 2014-01-04
2014-01-06 - 2014-01-07
2014-01-09
2014-01-11

のでRubyスクリプトを書いた。

require 'csv'

file_name = 'test.csv'
table = CSV.table(file_name, {:converters => :date, :headers => false})

last_date = nil
diff = 0

table.map { |row| row[0] }.chunk { |date|
  case
  when last_date.nil?, last_date + diff != date
    last_date = date
    diff = 1
  else
    diff += 1
  end

  last_date.to_s
}.each { |key, arr|
  if arr.length > 1
    puts "#{arr.first} - #{arr.last}"
  else
    puts "#{key}"
  end
}

実行結果

2014-01-01 - 2014-01-04
2014-01-06 - 2014-01-07
2014-01-09
2014-01-11