Here is a very traditional imperative solution.

def format_episodes_list(src)
  res = []
  j = 0
  while j < src.size
    i = j
    j += 1 while j+1 < src.size &&
                 src[j+1] == src[j].sub(/\d+/) { $&.succ }
    res << ((i == j) ? src[i] : "#{src[i]}-#{src[j]}")
    j += 1
  end
  res.join(", ")
end

puts format_episodes_list(['1', '2', '3', '4', '6', '7', '9', 'S1', 'S2' 
])
puts format_episodes_list([ '1', '2', 'S3', 'S4', 'S5', 'O6' ])
-- 
Posted via http://www.ruby-forum.com/.